如何让精灵坐在移动的精灵上

2024-01-10

如何让精灵坐在移动的精灵上并与其一起旅行。我已经让红色盒子随着冲动而跳跃,当它落到正在移动的黑色块上时,红色盒子会保持原状,掉落时会滑动移动物体,就像没有摩擦力一样。在重力、摩擦力均为 1.0 的情况下,两者甚至尝试增加质量,但没有任何效果。请给我任何详细信息如何使其发挥作用? 谢谢 覆盖 func didMoveToView(视图: SKView) { /* 在这里设置你的场景 */

    self.physicsWorld.gravity = CGVectorMake(0.0, -9.8)
    SquareOne.fillColor = UIColor.orangeColor()
    SquareOne.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    SquareOne.physicsBody = SKPhysicsBody(rectangleOfSize: SquareOne.frame.size)
    SquareOne.physicsBody?.friction = 1.0
    SquareOne.physicsBody?.restitution = 0.0

    addChild(SquareOne)


    SquareTwo.fillColor = UIColor.greenColor()
    SquareTwo.position = CGPoint(x: self.frame.midX, y: self.frame.midY - 100)
    SquareTwo.physicsBody = SKPhysicsBody(rectangleOfSize: SquareTwo.frame.size)
    SquareTwo.physicsBody?.dynamic = false
    SquareTwo.physicsBody?.affectedByGravity = false
    SquareTwo.physicsBody?.friction = 1.0
    SquareTwo.physicsBody?.restitution = 0.0
    addChild(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.runAction(SKAction.moveByX(-100, y: 0.0, duration: 3.0))
    }
}

在物理模拟中,您应该应用速度和力以使模拟正常工作。无法正确模拟物理SKAction.

首先是SquareTwo需要使其动态化才能受到力量的影响。使质量为SquareTwo大,这样身体就不会受到其他的影响SquareOne与它碰撞。

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

然后里面touchesBegan您可以将速度设置为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)
     }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让精灵坐在移动的精灵上 的相关文章

随机推荐