How to make sprite sit on a moving sprite

后端 未结 4 1891
暗喜
暗喜 2021-01-22 04:19

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

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-22 05:12

    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)
         }
    }
    

提交回复
热议问题