将物理实体添加到Spritekit节点

时间:2015-11-29 14:49:04

标签: ios swift sprite-kit skspritenode skphysicsbody

我正在使用Xcode 7和Swift 2在iOS上制作基本游戏。我正在尝试向节点添加物理实体。在用户的角色上,物理运行正常(没有崩溃,它运行相同)。但是,当向障碍物添加物理时,障碍物不会出现......这是我的障碍代码:

        let random = Int(arc4random_uniform(4))
        var objectTexture = SKTexture()
        var object = SKSpriteNode()
        if random == 0 {
            objectTexture = SKTexture(imageNamed: "coin")
            object = SKSpriteNode(texture: objectTexture)

            object.physicsBody = SKPhysicsBody(rectangleOfSize: objectTexture.size())
            object.physicsBody!.categoryBitMask = ColliderType.Coin.rawValue
            object.physicsBody!.contactTestBitMask = ColliderType.Coin.rawValue
            object.physicsBody!.collisionBitMask = ColliderType.Coin.rawValue

        }
        if random != 0 {
            objectTexture = SKTexture(imageNamed: "fryingPan")
            object = SKSpriteNode(texture: objectTexture)

            object.physicsBody = SKPhysicsBody(rectangleOfSize: objectTexture.size())
            object.physicsBody!.categoryBitMask = ColliderType.Obstacle.rawValue
            object.physicsBody!.contactTestBitMask = ColliderType.Obstacle.rawValue
            object.physicsBody!.collisionBitMask = ColliderType.Obstacle.rawValue

        }
        //Setting up object

使用此代码,对象不会出现在屏幕上。但是,当我注释掉物理线条时,对象会按照指示出现在屏幕上。有谁知道我可能会出错?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

object.physicsBody.affectedByGravity = false

我猜重力将你的障碍物移出屏幕