gamequery collision detection doesnt react - gamequery

i created 2 sprite objects and want to check if they collide, but it doesnt seem to work.
My code:
creation of the sprites:**
.addGroup("flying", {width: 366, height: 254})
.addSprite("flyinganimation",{animation: flight,
posx: 0, posy: 0, width: 366, height: 254})
.addGroup("obstacles", {width: 169, height: 67})
.addSprite("obstaclenames", {animation: obstacleimage,
width: 169,height: 67,
posx: 300,
posy: 400})
looped code in the callback function:**
var collision = $("#flying,.group").collision("#obstacles,.group");
if (collision > 0)
{
document.write ("collision");
}

I see two potential problems here:
1) collision() returns a jQuery object so you will need to do a .size() in order to retrieve the number of collisions
2) you should apply collision() to only one object.
To sum up your first line should look like this:
var collision = $("#flying").collision("#obstacles,.group").size();

Related

In SwiftUI, "onHover" does not call back when Path draws non-filled graphics

In SwiftUI, when I use Path to draw a non-filled graphic, for example, draw a hollow rectangle, as follows:
code show as below:
Path {
$0.addRect(CGRect(x: 100, y: 100, width: 200, height: 200))
}.stroke(Color.red,lineWidth: 10).onHover { isHover in
print(isHover)
}
when I set "onHover", when the mouse moves to the graph, the "onHover" method does not make a callback. Is there a good way to monitor the hover state of Path

Working with PDFKit: How to make page breaks?

Looking for some direction
I'm working with PDFKit. Everything is going fine but having trouble finding the methods (documentation / WWDC / elsewhere ) on how to stop text from drawing at a certain y position and start the next page. Any saved references or a snippet of code would be a great help.
I dont know, wether this is the best way to implement your use case, but it worked for me.
When adding a new line into the PDF_Context, I recalculate a variable the keeps tracking of the current content height of my PDF_Page. When it exceeds a certain value, I create a NEW page, set the content height to zero and go on filling, ...
And you might wanna find some good answers, practices HERE -> RayWenderlich.
// my initial value
private var myCurrentPageContentHeight: CGFloat = 20
// in your PDF fill procedures add something like
myCurrentPageContentHeight += 40
// I also have a struct the encapsulates
// PageSize with padding, ...
enum PDF_PageSize {
case din_A4_Portrait
case din_A4_Landscape
// -------------------------------
// getPDF_TotalRect
// -------------------------------
func getPDF_TotalRect() -> CGRect {
switch self {
case .din_A4_Portrait : return CGRect(x: 0, y: 0, width: 595, height: 842)
case .din_A4_Landscape : return CGRect(x: 0, y: 0, width: 842, height: 595)
}
}
// ....
}

removing array of GKGridGraphNodes from GKGridGraph unexpectedly slow

I am creating a 150 x 150 GKGridGraph for the purposes of A* pathfinding.
var graph = GKGridGraph(fromGridStartingAt: int2(0, 0), width: 150, height: 150, diagonalsAllowed: true)
I'm then looping through pixels of a reference map image and adding nodes to an array to be removed from the graph.
func getWalls(navMap: [bool], width: Int, height: Int) -> [GKGridGraphNode] {
var walls = [GKGridGraphNode]()
for(index, value) in navMap.enumberated() {
if(!value) { continue }
let x = index % width
let y = index / width
if let node = graph.node(atGridPosition: vector_int2(Int32(x),Int32(y))) {
walls.append(node)
}
}
return walls
}
up to this point everything is performant, but as soon as I attempt to remove the walls from the graph my program hangs for a LONG time.
graph?.remove(walls)
The thing that surprises me is that once the graph is setup, A* pathfinding on the grid runs super fast. While I expected pathfinding might be slow on such a large grid, I'm surprised that simply deleting a large chunk of nodes would cause such a significant performance hit.
So my question is WHY would removing a collection of nodes from GKGridGraph cause such a slowdown, and is there a more performant way to implement this operation?

Changing sprites using Sprite.Create does not take effect?

I'm making minecraft-like 2D game and everything seems like it's okay except changing block texture in one chunk. This is how I try to change sprite:
var block = new GameObject("blok", typeof(SpriteRenderer), typeof(BoxCollider2D), typeof(CircleCollider2D));
block.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("/Prefabs/Blokovi/blok_zemlja") as Texture2D, Rect(0, 240, 16, 16), Vector2(0.5, 0.5), 16);
It does not take any effect, why?
P.S. I use UnityScript/JavaScript.
Thanks in advance!
You do not need to use Instantiate() to create new empty GameObject. You are doing this right by using only constructor. To populate the GameObject by new components try to use AddComponent() method instead of putting the types to constructor. This method returning type is Component so you can easily get reference to just added component. Small example:
var block = new GameObject("block");
block.AddComponent("BoxCollider2D");
block.AddComponent("CircleCollider2D");
var spriteRenderer = block.AddComponent("SpriteRenderer");
spriteRenderer.sprite = Sprite.Create(Resources.Load("/Prefabs/Blokovi/blok_zemlja") as Texture2D, Rect(0, 240, 16, 16), Vector2(0.5, 0.5), 16);
I solved my problem by adding an tag named "blok" to block GameObject and adding one for loop which then changes sprite for every type of block (gameobject with tag "blok"). Here is for loop code:
var go_list = GameObject.FindGameObjectsWithTag ("blok");
for (var object in go_list) {
}

Tween transition in Kinetic js

I've been using the old 'transitionTo()' in my program but since Kineticjs is using Tween, i am a bit lost.
I've tried a simple shape transition using Tween and i've got some issues:
If you drag the shape to another point before doing anything, then click on the button for the transition, the shape comes back to the original hard coded coordinates and then does the transition.
I want the shape to start the transition where it has been dropped.
2.The 1st time it will do the transition, but afterwards it won't take the whole duration. It will just shift to the end point of the transition, like mentioned here.
Some codes:
var rect = new Kinetic.Rect({
x: 100,
y: 100,
width: 100,
height: 50,
fill: 'green',
stroke: 'black',
strokeWidth: 2,
draggable: true
});
layer.add(rect);
stage.add(layer);
var tween = new Kinetic.Tween({
node: rect,
x: 200,
y: 200,
rotation: 0,
duration:5
});
jsFiddle provided above.
Any help will be appreciated; thanx :)
This is what I would propose to solve your problems :
var stage = new Kinetic.Stage({
container: 'container',
width: 700,
height: 300
});
var layer = new Kinetic.Layer();
var rect = new Kinetic.Rect({
x: 100,
y: 100,
width: 100,
height: 50,
fill: 'green',
stroke: 'black',
strokeWidth: 2,
draggable: true
});
layer.add(rect);
stage.add(layer);
document.getElementById('run').addEventListener('click', function() {
var tween = new Kinetic.Tween({
node: rect,
x: 200,
y: 200,
rotation: 0,
duration:5
});
tween.play();
}, false);
Just instantiate the Tween transition at the moment, you want to use it. Otherwise, the transition will start with the position at the moment you instantiated it.
Here is the fork of your fiddle with my proposal : http://jsfiddle.net/kMvzy/