2015-12-05 41 views
-2

我是新來的iOS遊戲開發,我一直在關注這個教程系列,如果有人不介意看一看這個視頻(接近尾聲)https://www.youtube.com/watch?v=s0hRlSlT6Zw&list=PL6gx4Cwl9DGDgp7nGSUnnXihbTLFZJ79B&index=34我已經複製了整個代碼,但如果你看看該功能的touchesBegan似乎迅速的新版本有比他不同的功能,用Swift開發iOS遊戲。 SKScene/SprikeKit更改?

這是他如何讀取

override func touchesBegan(touches: Set NSSet, 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 = ShooterScene(fileNamed: "ShooterScene") 
     self.view?.presentScene(shooterScene, transition: doors) 
    }) 
} 

}

礦山讀取

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 = ShooterScene(fileNamed: "ShooterScene") 
      self.view?.presentScene(shooterScene, transition: doors) 
     }) 
    } 

} 

override func update(currentTime: CFTimeInterval) { 
} 

GameViewController.swift

import UIKit 
import SpriteKit 

class GameViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    if let scene = GameScene(fileNamed:"GameScene") { 
     // Configure the view. 
     let skView = self.view as! SKView 
     skView.showsFPS = true 
     skView.showsNodeCount = true 

     /* Sprite Kit applies additional optimizations to improve rendering performance */ 
     skView.ignoresSiblingOrder = true 

     /* Set the scale mode to scale to fit the window */ 
     scene.scaleMode = .AspectFill 

     skView.presentScene(scene) 
    } 
} 

override func shouldAutorotate() -> Bool { 
    return true 
} 

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
     return .AllButUpsideDown 
    } else { 
     return .All 
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

一切都寫在方法是相同的,但我得到一個錯誤,除非我把!之後在shooterScene行self.view?.presentScene(shooterScene,transition:doors)

不管我是否添加!當我點擊屏幕時,轉換不會發生。有人可以提供解決方案嗎?

+0

請添加場景的整個代碼,沒有人想通過視頻查看和複製代碼,以嘗試和模擬您的問題。 – FredLoh

+0

添加了其餘的代碼。 – MrShemp31

+0

通過更多的代碼,我的意思是整個視圖控制器,以便很容易地模擬問題。 – FredLoh

回答

1

它不工作,因爲沒有檢測到觸摸。你需要的是:

override func touchesBegan(touches: Set<UITouch>?, withEvent event: UIEvent?) { 

    for touch: AnyObject in touches! { 

     let touchLocation = touch.locationInNode(self) 

     button.containsPoint(touchLocation) { 
      //Button pressed; move to next scene 
     } 
    } 
} 

這是適合我的代碼。