重力 属性 在 SpriteKit 场景中不起作用

Gravity property not working in SpriteKit Scene

import SpriteKit

class GameScene: SKScene {
 override func didMoveToView(view: SKView) {
    /* Setup your scene here */
 }

 override func touchesBegan(touches: Set<UITouch>, withEvent event:            UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)

        let sprite = SKSpriteNode(imageNamed:"Beach Ball-100")
        self.addChild(sprite)

        sprite.xScale = 0.5
        sprite.yScale = 0.5
        sprite.position = location
        sprite.color = UIColor.redColor()
        sprite.physicsBody = SKPhysicsBody(edgeLoopFromRect:self.frame)
        sprite.physicsBody?.affectedByGravity = true
        var action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

  }
  }

  }

上面的代码出现在我的场景中class。当我 运行 它时,由于重力 属性 设置为真,球对象没有按预期下落。请解释我做错了什么。

Physicsbody 错误。你需要精灵的框架而不是世界的框架:

sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.frame.size)

添加SKPhysicsContactDelegate:

class GameScene: SKScene, SKPhysicsContactDelegate 

didMoveToViewtouchesBegan 中定义您的 physicsWorld(就像您在示例中所做的那样)

physicsWorld.gravity = CGVectorMake(0, -1.0)
physicsWorld.contactDelegate = self

给你的节点适当的SKPhysicsBody大小

sprite.physicsBody = SKPhysicsBody(circleOfRadius: frame.size.width / 2)