使节点动态只有一个对象Swift和Spritekit

时间:2016-06-24 15:03:04

标签: ios swift sprite-kit

我正在创建一个太空射击游戏,我希望我的一个节点只有在与特定节点交互时才是动态的,而不是所有节点。本质上,我只希望节点在与一个指定节点交互时充当动态。我怎么能这样做?

感谢。

我知道这个问题很令人困惑,但如果有人有任何想法,那将会非常有帮助!!

let Bullet = SKSpriteNode(imageNamed: "BulletGalaga.png")
            Bullet.zPosition = -5

            Bullet.position = CGPoint(x: ship.position.x, y: ship.position.y)
            Bullet.zRotation = ship.zRotation


            //let action = SKAction.moveTo(CGPointMake(400 * cos(Bullet.zRotation),(400 * -sin(Bullet.zRotation))), duration: 0.8)

            let action = SKAction.move(
                to: CGPoint(
                    x: 1200 * -cos(Bullet.zRotation - 1.57079633) + Bullet.position.x,
                    y: 1200 * -sin(Bullet.zRotation - 1.57079633) + Bullet.position.y
                ),
                duration: 2.4)

            //let action = SKAction.moveToY(self.size.height + 30, duration: 0.8)
            //let action = SKAction.moveTo(self.ship.size.height, duration: 0.8)
            let actionDone = SKAction.removeFromParent()
            Bullet.run(SKAction.sequence([action, actionDone]), withKey: "bulletAction")

            Bullet.physicsBody = SKPhysicsBody(rectangleOf: Bullet.size)
            Bullet.physicsBody?.affectedByGravity = false
            Bullet.physicsBody?.isDynamic = false
            self.addChild(Bullet)

我补充说:

    ship.physicsBody?.categoryBitMask = shipCategoryBitMask
    Bullet.physicsBody?.categoryBitMask = bulletCategoryBitMask
    enemy.physicsBody?.categoryBitMask = enemyCategoryBitMask
    enemyBullet.physicsBody?.categoryBitMask = enemyBulletCategoryBitMask

    ship.physicsBody?.collisionBitMask = enemyBulletCategoryBitMask
    Bullet.physicsBody?.collisionBitMask = enemyCategoryBitMask
    enemyBullet.physicsBody?.collisionBitMask = shipCategoryBitMask
    enemy.physicsBody?.collisionBitMask = bulletCategoryBitMask

1 个答案:

答案 0 :(得分:0)

一旦设置了SpriteNode的物理体,就可以使用collisionBitMask来指定它将与之碰撞的其他SpriteNodes。您需要先设置每个节点的物理主体的categoryBitMask。

let node1CategoryBitMask = 0x1 << 0
let node2CategoryBitMask = 0x1 << 1
let node3CategoryBitMask = 0x1 << 2

node1.physicsBody.categoryBitMask = node1CategoryBitMask
node2.physicsBody.categoryBitMask = node2CategoryBitMask
node3.physicsBody.categoryBitMask = node3CategoryBitMask

此时,三个精灵都设置了categoryBitMask。这就是物理机构的标识。现在设置collisionBitMasks。

node1.collisionBitMask = node2CategoryBitMask
node2.collisionBitMask = node1CategoryBitMask | node3CategoryBitMask

这告诉node1与node2冲突,而不是与node3冲突。 Node2将与node1和node3冲突。

希望这有帮助。