2015-08-08 69 views
0

我一直在迅速構建我的第一款遊戲,並且非常享受學習過程。然而,我不明白爲什麼我的主角cat誰動畫給出了一個運行的外觀(兩個圖像),只有在第一場比賽進行時才動畫。Swift iOS - 角色動畫在遊戲重置時爲靜態

重置功能後,貓在遊戲結束時停留在一個圖像上。最終貓不會動畫,除非它是應用程序加載後的第一個轉動。

以下是我認爲有用的所有代碼,請有人可以幫我嗎?

import SpriteKit 

class GameScene: SKScene, SKPhysicsContactDelegate { 

    var cat = SKSpriteNode() 
    var moving = SKNode() 
    var canRestart = false 

override func didMoveToView(view: SKView) { 

    self.addChild(moving) 

    var catTexture1 = SKTexture(imageNamed: "Cat1") 
    catTexture1.filteringMode = SKTextureFilteringMode.Nearest 
    var catTexture2 = SKTexture(imageNamed: "Cat2") 
    catTexture2.filteringMode = SKTextureFilteringMode.Nearest 

    var anim = SKAction.animateWithTextures([catTexture1, catTexture2], timePerFrame: 0.2) 
    var run = SKAction.repeatActionForever(anim) 

    cat = SKSpriteNode(texture: catTexture1) 
    cat.position = CGPoint(x: self.frame.size.width/2.2, y: self.frame.size.height/7.0) 
    cat.runAction(run) 

    moving.addChild(cat) 

} 


func resetScene() { 
    cat.position = CGPoint(x: self.frame.size.width/2.2, y: self.frame.size.height/7.0) 
    cat.physicsBody?.velocity = CGVectorMake(0, 0) 
    cat.physicsBody?.collisionBitMask = crowCategory | worldCategory; 
    cat.speed = 1.0; 
    cat.zRotation = 0.0; 

    crows.removeAllChildren() 
    trees.removeAllChildren() 


    canRestart == false 

    moving.speed = 1 

    score = 0; 
    scoreLabelNode.text = "\(score)" 

} 

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


    if (moving.speed > 0){ 
     cat.physicsBody!.velocity = CGVectorMake(0, 0) 
     cat.physicsBody!.applyImpulse(CGVectorMake(0, 20)) 
       } else if (canRestart) { 
        self.resetScene() 
    } 

    func didBeginContact(contact: SKPhysicsContact) { 

    if(moving.speed > 0) { 

     if((contact.bodyA.categoryBitMask & scoreCategory) == scoreCategory || (contact.bodyB.categoryBitMask & scoreCategory) == scoreCategory){ 

      score++ 
      scoreLabelNode.text = "Score \(score)" 
     }else { 

      moving.speed = 0; 
} 
} 
} 

我試圖重新找到所有種類的代碼沒有真正的成功,如果有人知道的解決方案,將是巨大的,快樂的發佈更多的代碼,如果有人,就可能會是有益的。

謝謝。

+0

您是否將'moving'或'cat'的'speed'屬性設置爲零的某處?這將停止動畫。另外,'canRestart == false'是一個NOP。 – 0x141E

+0

@ 0x141E感謝您的回覆,我設置'moving.speed = 0'的唯一時間是聯繫後。如果我改變這一點,遊戲不會暫停準備重新開始。有什麼我需要添加到我的'resetScene'? –

+1

當在「resetScene」中暫停動畫和「moving.paused = false」時,您可以設置「moving.paused = true」。順便說一句,你在哪裏設置'canRestart = true'? – 0x141E

回答

0

您可以使用鍵創建runAction。

然後在這樣的復位功能中刪除該鍵。

開始行動這樣的: -

SKAction *spriteAnimation = [SKAction animateWithTextures:Textures timePerFrame:0.04]; 
repeatWalkAnimation = [SKAction repeatActionForever:spriteAnimation]; 
[sprite runAction:repeatWalkAnimation withKey:@"animation1"]; 

停止操作是這樣的: -

[sprite removeActionForKey:@"animation1"]; 

希望這有助於。