【问题标题】:Node not moving once Physics added to scene: swift, SpriteKit将物理添加到场景后节点不移动:swift,SpriteKit
【发布时间】:2014-07-19 18:16:48
【问题描述】:

每次我添加以下代码:

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 */
    }
}

【问题讨论】:

  • 基于边缘的物体是静态的,它们不会移动

标签: ios swift sprite-kit


【解决方案1】:

此行导致问题:

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

你需要使用

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

上一行创建的physicsBody是场景的大小,因此它没有空间移动!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    相关资源
    最近更新 更多