2016-04-08 52 views
1

我是Swift語言的新手,我正在學習使用過時的swift語言的教程。該教程是關於用png文件創建動畫的。所以總共有6個png文件創建了一個動畫。這是教程「https://www.youtube.com/watch?v=5aCVyrJbrHQ」的鏈接。我注意到舊版本Swift和新版本中的一個重要區別。這裏是我卡住的地方。動畫無法在Swift中工作2.2

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let shooterNode = self.childNodeWithName("shooterNode") 

    if(shooterNode != nil) { 
     let animation = SKAction.animateWithTextures(shooterAnimation, timePerFrame: 0.1) 
     shooterNode?.runAction(animation) 
    } 

新版本:覆蓋FUNC的touchesBegan(觸摸:設置,withEvent事件:的UIEvent

舊版本:覆蓋FUNC的touchesBegan(觸摸:NSSet中,withEvent事件:的UIEvent

錯誤

只要我運行模擬器,然後點擊對象,我就沒有任何反應,沒有錯誤,只是沒有發現任何錯誤,我檢查了是否我用對象名稱做了一個錯字,但沒有發現任何錯誤。

這是情況下,整個文件的完整代碼,你會問它

import UIKit 
import SpriteKit 

class ShooterScene: SKScene { 

    var score = 0 
    var enemyCount = 10 
    var shooterAnimation = [SKTexture]() 

    override func didMoveToView(view: SKView) { 
     self.initShooterScene() 
    } 

    func initShooterScene(){ 
     let shooterAtlas = SKTextureAtlas(named: "shooter") 

     for index in 1...shooterAtlas.textureNames.count { 
      let imgName = String(format: "shooter%01d", index) 
      shooterAnimation += [shooterAtlas.textureNamed(imgName)] 

     } 
    } 
     //Anime the shooter 
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     let shooterNode = self.childNodeWithName("shooterNode") 

     if(shooterNode != nil) { 
      let animation = SKAction.animateWithTextures(shooterAnimation, timePerFrame: 0.1) 
      shooterNode?.runAction(animation) 
     } 
    } 
} 

GameSwift.scene

import SpriteKit 

class GameScene: SKScene { 
    override func didMoveToView(view: SKView) { 


    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     let introLabel = childNodeWithName("introLabel") 

     if(introLabel != nil) { 
      let fadeOut = SKAction.fadeOutWithDuration(1.5) 

      introLabel?.runAction(fadeOut, completion: { 
       let doors = SKTransition.doorwayWithDuration(1.5) 
       let shooterScene = SKScene(fileNamed: "ShooterScene") 
       self.view?.presentScene(shooterScene!, transition: doors) 

      }) 

     } 
    } 

    /* Called before each frame is rendered */ 
    override func update(currentTime: CFTimeInterval) { 
      } 
} 
+0

您是否檢查過每個方法是否都應該執行。在每個方法中放入一些打印語句,並確定是否所有內容都被正確調用。那將是一個開始。同時在'if(shooterNode!= nil){...}'塊中放置一個print語句。 – Whirlwind

+0

@Whirlwind感謝您的迴應,我在文件中的每個功能上添加了打印,控制檯中沒有任何內容出現。所以我得出的結論是,我的ShooterScene.sks和ShooterScene.swift文件之間存在某種聯繫。 –

+0

你在哪裏出現'ShooterScene'?實際創建一個新的場景(及其轉換)應該看起來像這樣:'if let nextScene = ShooterScene(fileNamed:「ShooterScene」){/ *這裏有可選轉換的場景* /}'所以檢查那個部分。 – Whirlwind

回答

2

它應該是:

let shooterScene = ShooterScene(fileNamed: "ShooterScene") 

,而不是:

let shooterScene = SKScene(fileNamed: "ShooterScene") 
+0

非常感謝!有人在評論中對這行代碼非常誤導:(但是,謝謝bro! –

+1

@CecilBoye沒問題:)實際上這是一個常見的錯誤。 – Whirlwind