簡體   English   中英

物理添加到場景后,節點不動:swift,SpriteKit

[英]Node not moving once Physics added to scene: swift, SpriteKit

每次我添加以下代碼:

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

這行引起問題:

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

您需要使用

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

在上一行中創建的PhysicsBody就是場景的大小,因此它沒有移動的空間!

您可以通過在showsPhysics屬性設置為YES來檢查正在創建的SKView 在此處查看更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM