How to make sprite sit on a moving sprite

后端 未结 4 1890
暗喜
暗喜 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:02

    If you have an skaction that has a duration, it keeps the object moving until the duration is over, creating the object to not stay on another object. So rather than using a action to move the object, update its position with the update function, and make it stop moving when it touches a block.

    0 讨论(0)
  • 2021-01-22 05:09

    You can remove the red block then add it as a child node to the moving block:

    var redBlockPosition = childNodeWithName("redBlockName")?.position //find where red block is in self
    var movingBlockPosition = childNodeWithName("movingBlockName")?.position //find where moveing block is in self
    var redBlockPositionFromMovingBlock = CGPointMake(redBlockPosition.x + movingBlockPosition.x, redBlockPosition.y - movingBlockPosition.y) //find where red block is from moving block's origin
    childNodeWithName("redBlockName")?.removeFromParent() //remove red block
    redBlock.position = redBlockPositionFromMovingBlock //change redBlock's position
    childNodeWithName("movingBlockName")?.addChild(redBlock) //add red block as a child node of moving block
    

    Hope this helped and good luck.

    0 讨论(0)
  • 2021-01-22 05:10

    Try redBox.physicsBody.allowsRotation = NO,

    A Boolean value that indicates whether the physics body is affected by angular forces and impulses applied to it.

    And adjust redBox.physicsBody.restitution between 0.0 to 1.0,

    The bounciness of the physics body.

    0 讨论(0)
  • 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<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */
    
        for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)
            SquareTwo.physicsBody?.velocity = CGVectorMake(-10, 0)
         }
    }
    
    0 讨论(0)
提交回复
热议问题