物理学添加到场景后节点不移动:swift,SpriteKit

时间:2014-07-19 18:16:48

标签: ios swift sprite-kit

每次我添加以下代码:

self.physicsBody = SKPhysicsBody(edgeLoopF​​romRect:self.frame)

我的"球的物理机构"项目停止移动。它在添加上一行之前有效。

这是我的所有代码:

class GameScene: SKScene {
    // creaTE nODE/Sprite
    let ball = SKSpriteNode(imageNamed: "ball.png")
    override func didMoveToView(view: SKView) {
        // set up scene here
        self.backgroundColor = SKColor.whiteColor()
        //add physics to scene
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        //create cg piont for sprite
        ball.position = CGPointMake(size.width / 2, size.height / 2)
        //give it some real life physics!
        ball.physicsBody = SKPhysicsBody(circleOfRadius: size.height / 2)
        addChild(ball)
    }
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {}
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

1 个答案:

答案 0 :(得分:0)

此行导致问题:

ball.physicsBody = SKPhysicsBody(circleOfRadius: size.height / 2)

您需要使用

ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height / 2)

在前一行中创建的physicsBody是场景的大小,因此没有空间可以移动!

您可以通过在showsPhysics中将YES属性设置为SKView来检查正在创建的physicsBody。查看here了解详情。