How to make a sprite sit on a moving sprite and travel with it. I have made the red box jump with impulse and when it falls on the black block down which is moving, the red box
In a physics simulation, you should apply velocity and forces for the simulation to work correctly. Physics cannot be simulated correctly using SKAction
.
First the SquareTwo
needs to be made dynamic for it to be effected by forces. Make the mass of SquareTwo
big so that the body is not affected by the other SquareOne
colliding with it.
SquareTwo.physicsBody?.dynamic = true
SquareTwo.physicsBody?.affectedByGravity = false
// Make the mass big so that the body is not affected by the other body colliding with it.
SquareTwo.physicsBody?.mass = 1000000
Then inside touchesBegan
you can set a velocity to SquareTwo
override func touchesBegan(touches: Set, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set) {
let location = touch.locationInNode(self)
SquareTwo.physicsBody?.velocity = CGVectorMake(-10, 0)
}
}