当我的heroNode与Swift Spritekit中的敌人节点联系时,我如何暂停我的背景?

时间:2015-05-08 16:19:15

标签: swift sknode

当我的英雄节点与敌人接触时,我正试图暂停我的背景。当我使用这个代码时,我在下面张贴了背景的速度并没有停止它保持正常速度。我曾尝试在全球范围内声明“城市”但我收到错误Attempted to add a SKNode which already has a parent。我做错了什么?

class GameScene: SKScene, SKPhysicsContactDelegate {    
    let city = SKSpriteNode(imageNamed: "skyline")
}

override func didMoveToView(view: SKView) {
    repeatCity()
}


func addCity() {
    let city = SKSpriteNode(imageNamed: "skyline")
    let moveToRight = SKAction.moveByX(-1000, y: 0, duration: 5.0)
    let repeatAction = SKAction.repeatActionForever(moveToRight)



    city.position = CGPointMake(self.size.width / 0.7, self.size.height / 1.9)
    city.zPosition = 13
    city.setScale(0.9)
    city.runAction(repeatAction)
    addChild(city)


}

func repeatCity() {
    let generateBlocks = SKAction.sequence([
        SKAction.runBlock(self.addCity),
        SKAction.waitForDuration(3.5)])
    let endlessAction = SKAction.repeatActionForever(generateBlocks)
    runAction(endlessAction)

}
    func didBeginContact(contact:SKPhysicsContact){

    if firstBody.categoryBitMask == HeroCategory && fourthBody.categoryBitMask == EnemyCategory {

        city.speed = 0
        theHero.physicsBody?.affectedByGravity = true
        println("contactwithEnemy!!!")

    }
}

1 个答案:

答案 0 :(得分:0)

第一个问题是,您声明了一个类变量city但是在add方法中,您不使用类变量而是使用自己的城市变量。所以你应该删除那个类变量:

let city = SKSpriteNode(imageNamed: "skyline")

现在,您在city节点上重复执行的操作一直在运行。要停止该操作,您应该从联系人city节点中删除所有操作:

self.removeAllActions()