在碰撞时删除Sprite并保留相同sprite的副本

时间:2014-12-10 17:23:55

标签: swift sprite-kit collision skspritenode flappy-bird-clone

我创建了一个应用Flappy Bird的克隆,我试图添加在游戏中随机出现的硬币。我的问题是,当鸟与它们发生碰撞时,我试图让硬币消失,但事实证明这是一项艰巨的任务。我有一个每3秒产生一枚硬币的功能;起初所有的硬币都有相同的categoryBitMask,但后来我改了它,所以每3个硬币都有classBitMask 1,2,3,重复出现。

我在代码上尝试了许多细微变化,以便在与鸟碰撞时摆脱硬币,有时硬币会消失,但有其他问题,如错误的硬币消失或没有更多的硬币产卵。以下是我认为您需要查看的所有代码:

class GameScene: SKScene, SKPhysicsContactDelegate {


    var bird = SKSpriteNode()
    var coin = SKSpriteNode()

    let birdGroup: UInt32 = 1
    let objectGroup: UInt32 = 2
    let gapGroup: UInt32 = 0 << 3
    let gapGroup2: UInt32 = 0 << 4
    let gapGroup3: UInt32 = 0 << 5

    var gameOver = 0

    var movingObjects = SKNode()

    var coinGroup1 = SKNode()
    var coinGroup2 = SKNode()
    var coinGroup3 = SKNode()



   override func didMoveToView(view: SKView) {



/********| Bird physics body |********/

        bird.physicsBody?.categoryBitMask = birdGroup
        bird.physicsBody?.collisionBitMask = gapGroup
        bird.physicsBody?.collisionBitMask = gapGroup2
        bird.physicsBody?.collisionBitMask = gapGroup3
        bird.physicsBody?.collisionBitMask = objectGroup
        bird.physicsBody?.contactTestBitMask = objectGroup

        bird.zPosition = 10

        self.addChild(bird)

/********| Bird physics body |********/



/********| Spawning coins |********/

  var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("spawnPipes"), userInfo: nil, repeats: true)

}
 func spawnPipes(){


        var coinTexture = SKTexture(imageNamed: "coin.png")
        coin = SKSpriteNode(texture: coinTexture)
        coin.size.height = bird.size.height
        coin.size.width = bird.size.width
        coin.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width * 1.33, y: CGRectGetMidY(self.frame) + movementAmount2)
        coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2)
        coin.physicsBody?.dynamic = false
        coin.runAction(moveAndRemove)


        coin.physicsBody?.contactTestBitMask = birdGroup


            if(i == 1){
                coin.physicsBody?.categoryBitMask = gapGroup
                coin.physicsBody?.collisionBitMask = gapGroup
                coinGroup1.addChild(coin)

            }else if(i == 2){
                coin.physicsBody?.categoryBitMask = gapGroup2
                coin.physicsBody?.collisionBitMask = gapGroup2
                coinGroup2.addChild(coin)

            }else if(i == 3){
                coin.physicsBody?.categoryBitMask = gapGroup3
                coin.physicsBody?.collisionBitMask = gapGroup3
                coinGroup3.addChild(coin)

            }else{
                i = 1


        }

    }

/********| Spawning coins |********/



/********| Removing coins |********/

func didBeginContact(contact: SKPhysicsContact) {

        if(gameOver == 0){
            if((contact.bodyA.categoryBitMask == gapGroup || contact.bodyB.categoryBitMask == gapGroup) || (contact.bodyA.categoryBitMask == gapGroup2 || contact.bodyB.categoryBitMask == gapGroup2) || (contact.bodyA.categoryBitMask == gapGroup3 || contact.bodyB.categoryBitMask == gapGroup3)){

            score++

            scoreLabel.text = "\(score)"
                if(contact.bodyA.categoryBitMask == gapGroup || contact.bodyB.categoryBitMask == gapGroup){
                    coinGroup1.removeAllChildren()
                }else if(contact.bodyA.categoryBitMask == gapGroup2 || contact.bodyB.categoryBitMask == gapGroup2){
                    coinGroup2.removeAllChildren()
                }else if(contact.bodyA.categoryBitMask == gapGroup3 || contact.bodyB.categoryBitMask == gapGroup3){
                    coinGroup3.removeAllChildren()
                }


/********| Removing coins |********/

如果这个^^^还不够,这是我的所有代码:

http://pastebin.com/7yGh5gBn

0 个答案:

没有答案