如何让精灵坐在移动的精灵上 [英] How to make sprite sit on a moving sprite
问题描述
如何让精灵坐在移动的精灵上并与它一起旅行.我已经让红色盒子冲动地跳起来,当它落在正在移动的黑色块上时,红色盒子会停留在它掉下来的情况下,就像没有摩擦一样滑动移动的物体.在重力和摩擦力 1.0 的条件下,甚至尝试增加质量,但没有任何效果.请给我任何细节如何使它工作?谢谢覆盖函数 didMoveToView(视图:SKView){/* 在这里设置你的场景 */
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 stays were it dropped slips the moving object like there is no friction. Conditions gravity on, friction 1.0 in both even tried increasing the mass, but nothing worked. Please give me any details how to make it work ? thanks override func didMoveToView(view: SKView) { /* Setup your scene here */
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
无法正确模拟物理.
In a physics simulation, you should apply velocity and forces for the simulation to work correctly. Physics cannot be simulated correctly using SKAction
.
首先需要将 SquareTwo
设置为动态的,以使其受到力的影响.使 SquareTwo
的质量大,这样身体就不会受到其他 SquareOne
与之碰撞的影响.
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
然后在 touchesBegan
中,您可以将速度设置为 SquareTwo
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)
}
}
这篇关于如何让精灵坐在移动的精灵上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!