J Norris J Norris - 6 months ago 30
Swift Question

Touch Sprite, make it jump up then fall down again(repeat as many times as spritenode is tapped.)

I created a project where I have a ball and when the view loads, it falls down, which is good. I'm trying to get the ball to jump back up and fall down again when the Spritenode is tapped.

--This Question was edited--
Originally, I was able to get it to work when

sprite.userInteractionEnabled = false
. I had to turn this statement true in order to get the score to change.

enter image description here

Now I can't get the balls to fall and be tapped to jump. When I turn
ball.physicsBody?.dynamic = true
, the balls will fall due to gravity. How do I tap the sprite itself and make it jump.


override func didMoveToView(view: SKView) {
let ball = Ball()

ball.position = CGPoint(x:self.size.width / 2.0, y: 440)


ball.physicsBody = SKPhysicsBody(circleOfRadius: 120)
ball.physicsBody?.dynamic = true
ball.physicsBody?.allowsRotation = false

ball.physicsBody?.restitution = 3
ball.physicsBody?.friction = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0

ball.physicsBody?.usesPreciseCollisionDetection = true

class Ball: SKSpriteNode {

init() {
let texture = SKTexture(imageNamed: "Ball")
super.init(texture: texture, color: .clearColor(), size: texture.size())
userInteractionEnabled = true


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let scene = self.scene as! GameScene
scene.score += 1


Before, it was a SKNode being tapped using
(impulse, velocity) now, it's a SKSpriteNode, and I tried using
, but it either does not work, or I'm putting it in the wrong place(touches begin).


I tested your code and it seems that using firstBall.userInteractionEnabled = true is the cause. Without it, it should work. I did some research (here for example), but can't figure out what's the reason of this behavior with userInteractionEnabled. Or for what reason do you use userInteractionEnabled?