I am developing the iPhone version of an Android game using SpriteKit and objective-c. The Android version was developed using libGDX. There the movement across the screen was done by updating the coordinate values whenever the predefined render method is called. While reading the docs in SpriteKit, I found that here the nodes are moved across the screen by using SKAction where the end coordinates and the duration is given and the action is applied to the node. Is this the only way the node can be moved? Or can I update the coordinates(in the update method according to the time interval) even after the adding the node to the parent node(without calling the SKAction anywhere)?
2 Answers
There are a couple of ways to make a node move.
Changing the node's coordinates
myNode.position = CGPointMake(myNode.position.x+1, myNode.position.y);
will move the node to the right. This method works with regardless on whether they have a physics body or not.Apply velocity to the node's physics body
myNode.physicsBody.velocity = CGVectorMake(100, myNode.physicsBody.velocity.dy);
moves the node to the right. Your node requires a physics body for this to work.Apply an impulse
[myNode.physicsBody applyImpulse:CGVectorMake(100, myNode.physicsBody.velocity.dy)];
moves the node to the right. Your node requires a physics body for this to work.Apply a force
[myNode.physicsBody applyForce:CGVectorMake(100, myNode.physicsBody.velocity.dy)];
moves the node to the right. Your node requires a physics body for this to work.
When you apply force is like a motor pushing all the time. If you apply impulse it's like kicking a ball once.
Read up on each method in more detail in the SKPhysicsBody reference.
You can move a node by specifying the position
property (see the SKNode
documentation)
node.position = CGPointMake(x, y);