我是iOS游戏开发的新手,我一直在关注这个教程系列,如果有人不介意看一下这个视频(接近结尾)https://www.youtube.com/watch?v=s0hRlSlT6Zw&list=PL6gx4Cwl9DGDgp7nGSUnnXihbTLFZJ79B&index=34我已经复制了整个代码但如果你看一下touchesBegan函数,似乎较新版本的swift与他的功能不同,
以下是他的阅读方式
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()
}
用该方法编写的所有东西都是一样的,但除非我放一个,否则我会收到错误!射手游戏之后的自我观点?.presentScene(射手游戏,转型:门)
无论我是否添加!当我点击屏幕时,不会发生转换。有人可以提供解决方案吗?
答案 0 :(得分: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
}
}
}
这是适用于我的代码。