-1

How do I get the SkSpriteNode to move up when clicked? -Thanks

https://i.stack.imgur.com/fcOvr.png

Maddog
  • 11
  • 1
  • 2

2 Answers2

0

lets say your node is called box

use the touches began function

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for _ in touches{
        let touch:UITouch = touches.first!
        let touchLocation = touch.location(in: self)
        box.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        box.physicsBody?.applyImpulse(CGVector(dx: 0, dy:300))

    }

}

the applyImpusle method takes an x value to move along the x axis(left or right) and takes a y value to move along the y axis (up or down). set dy accordingly.

rest of it depends on your scene properties like gravity , friction , mass of the box and so on

Aman Kapoor
  • 90
  • 1
  • 6
0

Without using a physicsBody you can use this code and just change how you move it up (I do it manually just to keep it simple).

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        
        if sprite.contains(location){
            
            sprite.position = CGPoint(x: 0, y: 10)
        }
Dharman
  • 30,962
  • 25
  • 85
  • 135