How do I get the SkSpriteNode to move up when clicked? -Thanks
Asked
Active
Viewed 35 times
2 Answers
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
-
1This assumes people are using physics bodies, and you should be using `physicsBody!` not `physicsBody?` – Knight0fDragon Aug 06 '18 at 16:54
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

mollydrake
- 1
- 1