How to make a move node in a swift spriteset
I've been wondering how to do this for a long time. I am using Sprite Kit swift, my problem is I donโt know how to make a node move using SKActions so when you are on stage that I put it on a node reference (name <help node), I donโt understand how it is works, can someone please show me an explained example of how to do this, ahead of time!
source to share
To move Sprite to Sprite-Kit you can use SKActions.
For example:
let action = SKAction.moveByX(3, y: 2, duration: 10)
- This will cause the sprite to move 3 units along the x-axis and 2 units along the y-axis after 10 seconds.
If you want your sprite to move to a specific location, you can do:
let action2 = SKAction.moveTo(location: CGPoint, duration: NSTimeInterval)
Hope this helped!
source to share
You can give actions to Node or Sprite in Swift 3.0 ... First, we will resize the Node or Sprite.
let nodeSize = SKAction.scale(to: 0.5, duration: 2)
After two seconds, this will resize the object from half that size. Then, to move the Node or Sprite to a different location, use ...
let nodeSet = SKAction.moveBy(x: -3.0, y: 2.0, duration: 2)
After two seconds, the object will move 3 units to the left and 2 units up.
If you want to assign these actions to a specific Node, you can create the Node first var parentNode = SKShapeNode()
, and then you can tell them to fire the action.
parentNode.run(nodeSize)
parentNode.run(nodeSet)
Hope it helps.
source to share
As I understand it, you have a scene with some node (eg name = "myNode").
First of all, you need to access this node:
override func didMoveToView(view: SKView) {
let myNode = childNodeWithName(homeButtonName)!
...
}
You now have a link to your node.
The next step is to add an action to move this node. For example, move this node +20 horizontally and -30 vertically in 3 seconds:
let dX = 20
let dY = -30
let moveAction = SKAction.moveByX(CGFloat(dX), y: CGFloat(dY), duration: 3.0)
myNode.runAction(moveAction)
You can change many properties of a node, not just the position, eg. size, alpha, rotation, etc.
source to share