gpt4 book ai didi

swift - 调用过渡方法时,SKScene 返回 nil

转载 作者:行者123 更新时间:2023-11-28 08:35:48 25 4
gpt4 key购买 nike

在我的小 Xcode 项目中,当我的 SKLabelNode 被触摸时,我试图转换场景,它会呈现下一个场景。出于某种原因,在我的场景转换方法中,它让我将其存储为可选。并且可选返回 false。我的文件名是正确的,引用中没有语法错误会导致问题,这是我的代码。当我单击我的 SKLabelNode 时,运行它的 ios 平台识别触摸然后应用程序崩溃,在控制台中说可选值返回 nil。我该如何解决这个问题?谢谢

import SpriteKit

class GameScene: SKScene {

let next = SKScene(fileNamed: "nextScene")

override func didMoveToView(view: SKView) {


let backgroundimage = SKSpriteNode(imageNamed: "ipbg")
backgroundimage.size = CGSize(width: self.frame.size.width, height: self.frame.size.height)
backgroundimage.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
addChild(backgroundimage)

let playButton = SKLabelNode(fontNamed: "")
playButton.name = "play"
playButton.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2 + 100)
playButton.text = "Play"

let wait = SKAction.waitForDuration(2)
let run = SKAction.runBlock({


let randomNumber = Int(arc4random_uniform(UInt32(4)))

switch(randomNumber){

case (0):

playButton.fontColor = UIColor.blueColor()

case 1:

playButton.fontColor = UIColor.yellowColor()

case 2:

playButton.fontColor = UIColor.purpleColor()

case 3:

playButton.fontColor = UIColor.orangeColor()

default: print("default")


}

})

addChild(playButton)
var repeatActionForever = SKAction.repeatActionForever(SKAction.sequence([wait, run]))
runAction(repeatActionForever)
}

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

let trans = SKTransition.crossFadeWithDuration(1)
let touch = touches.first! as UITouch
let touchLocation = touch.locationInNode(self)
let touchedNode = nodeAtPoint(touchLocation)

if touchedNode.name == "play"{


scene!.view?.presentScene(next!, transition: trans)

}

}
}

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

最佳答案

您发布的代码无法编译,因此很难找到错误。

但是我会尝试用下面的方法替换 touchesBegan

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

let trans = SKTransition.crossFadeWithDuration(1)
let touch = touches.first! as UITouch
let touchLocation = touch.locationInNode(self)
let touchedNode = nodeAtPoint(touchLocation)

if touchedNode.name == "play"{
guard let nextScene = SKScene(fileNamed: "nextScene")
else { fatalError("Could not load nextScene") }
self.view?.presentScene(nextScene, transition: trans)
}
}

关于swift - 调用过渡方法时,SKScene 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com