Reputation: 3958
I have a SKSpriteNode which is not moving as expected:
In my GameScene.swift
var ball = SKSpriteNode(imageNamed: "Sphere")
ball.position = CGPointMake(frame.size.width/2, frame.size.height/2)
ball.physicsBody?.dynamic = true
self.addChild(ball)
var impulse = CGVectorMake(0.5, 0.0)
ball.physicsBody?.applyImpulse(impulse)
Now, I am able to see the sphere in the middle of the screen, but it is not moving as expected, why?
Not even with:
ball.physicsBody?.affectedByGravity = true
Any help is appreciated, thanks
Upvotes: 0
Views: 53
Reputation: 271
First thoughts I have to check the PhysicsBody
has been initialised - perhaps your optional ? unwrap is failing. So add in the following line below your var ball declaration:
ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.frame.size)
It may not be the ideal physics body shape, but this should allow your ball to move as per the impulse.
Upvotes: 2