2015-09-11 180 views
2

如何讓精靈坐在移動的精靈上並隨其移動。我讓紅色盒子衝動地跳起來,當紅色盒子落在正在移動的黑色塊子上時,紅色盒子停留在它掉落的地方,滑動移動的物體,就像沒有摩擦。條件引力,摩擦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)) 
    } 
} 

How i want it to work

+0

爲了使這一更加簡單,如果有兩個街區紅色和黑色。紅色受重力影響,黑色不會影響我的重力。觸摸開始功能一旦我觸摸黑色開始在x方向移動。當它移動紅色塊滑倒。如何使它附着在黑塊上。 –

+1

你有正確的想法。你只需要使用物理學來移動黑塊。這是如何http://stackoverflow.com/questions/31590885/how-to-move-platform-with-velocity – 0x141E

回答

2

在物理模擬中,您應該使用速度和力來模擬正常工作。使用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) 
    } 
} 
+0

感謝您的回答。有效。這是實施的唯一方法嗎? –

0

您可以刪除紅塊,然後將其添加爲一個子節點在移動塊:

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 

希望這會有所幫助,祝你好運。

+0

我不能刪除redblock,因爲它的主要塊,它不斷從一個黑塊跳轉到另一個。所以我需要刪除併爲每個移動的黑色塊添加紅色塊。我不覺得這是一種有效的方式。 –

+0

嘗試增加摩擦力至最大值,以免滑塊滑動。 –

0

嘗試redBox.physicsBody.allowsRotation = NO

一個布爾值,它指示物理體是否由 角度的力和施加到其上的脈衝的影響。

,並調整爲0.0〜1.0之間redBox.physicsBody.restitution

物理體的反彈力。

+0

它不起作用仍然紅色框滑走。 –

+0

@vaidhyanathan如果你提供一個樣本項目,我會看看。 – WangYudong

+0

我已經上傳到谷歌驅動器https://drive.google.com/file/d/0BwsEelZ-x-VKbnJCeXc3ZXhsWDA/view請讓我 –