Skspritenode Position Changes to 0,0 for No Reason

Which position value changes when you use an SKAction on an SKSpriteNode? And how would I account for the position changes?

Your gift.run(fall) is scheduling an action that will happen over the course of 0.2 seconds, not waiting for it to finish. At 60 frames/second, that means the sprite will reach its final position only after 12 frames. If you print the gift's position at each step in update(_:), you'll see that it is indeed changing.

Since the run does not wait for the action to complete, the sprite hasn't had a chance to move at all when the second print is executed.

If you want something to happen only after an action completes, you have various possibilities. A common way is to add a completion block; see Apple's documentation on getting started with actions: https://developer.apple.com/documentation/spritekit/getting_started_with_actions. Or if you want the same node to do multiple things in order, you can make a composite SKAction, like with the sequence or repeat initializers.

SKSpriteNode's position changes after placement

Alright.

I have done some research regarding this issue and have come to a conclusion that if a SKSpriteNode is parented to a SKNode and you set the SKSpriteNode's SKPhysicsObject's dynamic property to false, the object will still slowly keep drifting down. It shouldn't move at all with the dynamic property set to false. This bug doesn't occur if SKSpriteNode's parent is the SKScene.

This bug was reported in Apple developer forums thread at November 27th in 2015. It was said to be still existing on December 20th. I can now say that it still exists on 10th of July 2016. I have Xcode 7.0 and OS X 10.11.5 and I am only familiar with the OS X version of the spritekit, I don't know if the bug is on iOS also.

Originally my map system was designed so that the map is SKNode and the tiles are SKSpriteNodes. As a result of this bug, I changed it so that the SKSpriteNodes(tiles) are directly added to the SKScene. Now the bug doesn't occur.

Position SKSpriteNode at 0,0?

This is probably happening because your view size, and your scene size have different dimensions. I guess you are using default game project template where the scene is loaded from .sks file (scene size in that case is 1024x768). If scene has different size than a view even if the sprite is added, it might not be visible because it can be off-screen. You can test this by adding a sprite at other position (eg. x=300,y=300). One thing you can do is to simply change scene's size (inside view controller) like this: scene.size = skView.bounds.size Also the important thing here is scene's scale mode (from the docs):

After a scene is rendered, its contents are copied into the presenting
view. If the view and the scene are the same size, then the content
can be directly copied into the view. If the two differ, then the
scene is scaled to fit in the view. The scaleMode property determines
how the content is scaled.

On top of this, I would suggest you to read this about how to initialize your scene in view controller and there is also debate about viewDidLoad vs viewWillLayoutSubviews methods.

Unrelated to your problem, but you might find useful to know that changing anchor point will not affect on physics body of a node. It's a purely visual property.

Hope this helps.

Track the position of SKSpriteNode while doing a SKAction moveTo

You have already all you needed.

Suppose you have a reference for your node:

var sprite1: SKSpriteNode!

And you want to spawn it to a random position (an example method..):

self.spawnToRandomPos(sprite1)

And suppose you want to moveTo your sprite1:

self.sprite1.runAction( SKAction.moveToY(height, duration: 0))

Everytime you check his position you know where is it:

print(sprite1.position)

So to know always your sprite1 position you could do this code and you see all it's movements:

override func update(currentTime: CFTimeInterval) {
print(sprite1.position)
}

P.S.:

In case you dont have references about your object spawned you can also give a name to a generic object based for example by a word followed to a counter (this is just an example):

for i in 0..<10 {
var spriteTexture = SKTexture(imageNamed: "sprite.png")
let sprite = SKSpriteNode(texture: spriteTexture, size: spriteTexture.size)
sprite.name = "sprite\(i)"
self.addChild(sprite)
}

After this to retrieve/re-obtain your sprite do:

 let sprite1 = self.childNodeWithName("sprite1")

Sprite position not updating to where it is being moved to

it is working properly and the results are as expected.

position is the position within the parent. so in your case the sprites position within the SKNode which never changes.

Think of it this way if I asked you to pack a cooler, and said to pack Cokes on the left of the cooler and Coronas on the right. When we move that cooler to the camp ground the cokes still have the same position in the cooler. ;)

Edit

further following with my same example

if I want to find out the Coke's position in relation to the campground and not the cooler I would have to convert it.

using...

convert(_:to:) or convert(_:from:)

From Apple...

Converting Between Coordinate Spaces
When working with the node tree, sometimes you need to convert a position from one coordinate space to another. For example, when specifying joints in the physics system, the joint positions are specified in scene coordinates. So, if you have those points in a local coordinate system, you need to convert them to the scene’s coordinate space.
The following code shows how to convert a node’s position into the scene coordinate system. The scene is asked to perform the conversion. Remember that a node’s position is specified in its parent’s coordinate system, so the code passes node.parent as the node to convert from. You could perform the same conversion in reverse by calling the convert(_:to:) method.



Related Topics



Leave a reply



Submit