enumerateChildNodes在精灵工具包中找不到子节点-Swift 4
我正在尝试创建一个SpriteKit游戏,其中一个球在屏幕上移动。当球离开屏幕时,我想将其从父场景中移除,并切换到另一个场景(GameOverScene) 我正在使用enumerateChildNodes在精灵工具包中找不到子节点-Swift 4,swift,sprite-kit,Swift,Sprite Kit,我正在尝试创建一个SpriteKit游戏,其中一个球在屏幕上移动。当球离开屏幕时,我想将其从父场景中移除,并切换到另一个场景(GameOverScene) 我正在使用enumerateChildNodes,但它似乎不起作用。我不确定问题出在哪里,但我认为可能与父母/子女关系有关 func createBall(forTrack track: Int) { setupTracks() player?.physicsBody?.linearDamping = 0 pla
enumerateChildNodes
,但它似乎不起作用。我不确定问题出在哪里,但我认为可能与父母/子女关系有关
func createBall(forTrack track: Int) {
setupTracks()
player?.physicsBody?.linearDamping = 0
player = SKSpriteNode(imageNamed: "small")
player?.name = "BALL"
player?.size = CGSize(width: 100, height: 100)
ballValue = 1
randFloat = Float(arc4random()) / Float(UINT32_MAX)
if randFloat > 0.001 {
ballSpeed = randFloat / 50
}
else {
ballSpeed = randFloat / 50
}
let ballPosition = trackArray?[track].position
player?.position = CGPoint(x: (ballPosition?.x)!, y: (ballPosition?.y)!)
player?.position.y = (ballPosition?.y)!
if ballDirection == "right" {
player?.position.x = 0
moveRight()
}
else {
player?.position.x = (self.view?.frame.size.height)!
moveLeft(speed: ballSpeed)
}
self.addChild(player!)
self.enumerateChildNodes(withName: "BALL") { (node: SKNode, nil) in
if node.position.x < -100 || node.position.x > (self.size.width) + 100 {
print("balls Out")
node.removeFromParent()
let transition = SKTransition.fade(withDuration: 1)
self.gameScene = SKScene(fileNamed: "GameOverScene")
self.gameScene.scaleMode = .aspectFit
self.view?.presentScene(self.gameScene, transition: transition)
}
}
}
第二个在覆盖功能触摸开始时:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.previousLocation(in: self)
let node = self.nodes(at: location).first
if node?.name == "BALL" {
currentScore += ballValue
player?.removeFromParent()
createBall(forTrack: track)
}
else {
let transition = SKTransition.fade(withDuration: 1)
gameScene = SKScene(fileNamed: "GameOverScene")
gameScene.scaleMode = .aspectFit
self.view?.presentScene(gameScene, transition: transition)
}
}
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让位置=触摸。上一个位置(in:self)
让node=self.nodes(在:位置)。首先
如果节点?.name==“球”{
currentScore+=球值
player?.removeFromParent()
createBall(forTrack:track)
}
否则{
let transition=SKTransition.fade(持续时间:1)
GameSecene=SKScene(文件名为:“GameOverScene”)
gameScene.scaleMode=.aspectFit
self.view?.presentScene(游戏场景,过渡:过渡)
}
}
}
更新:
中的行self.enumerateChildNodes(with name:“BALL”){(node:SKNode,nil)起作用,因此它不是一个父子关系问题。if语句
不起作用。阅读您的代码我认为这不是一个解决方案,而是一种不同的方法。这些建议会让您的生活更轻松:
从较小的代码开始,避免或注释掉所有不需要的代码(如if-randFloat)
使用!强制展开player=SKSpriteNode(imagename:“small”)!
,因为如果初始化失败,您实际上想要崩溃,那么您就可以摆脱所有
在触摸开始时,最好使用进行触摸{
,因为它更易于管理
let location=touch.previousLocation(在:self中)
您的意思可能是let location=touch.location(在:self中)
当球离开屏幕时,我希望将其从父对象中移除,因此这是游戏中的某个时候发生的事情。您希望调用您的self.enumerateChildNodes(使用名称:“ball”){
,而不是每次触摸屏幕时
如果这还不够,请随意发布最少的代码来创建一个游乐场,让我来为您测试:我唯一无法工作的是2。我没有显示这一点,但我在顶部初始化了玩家varvar-player:SKSpriteNode?
当我用“!”切换“?”并删除所有其他的“!”时,它给出了“致命的Er”ror’。除此之外,这里的一切都很完美,任何读到这个问题的人都应该看看这个答案。这很奇怪,我一直都是为了这个目的而使用它的。谢谢你的评论:]
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.previousLocation(in: self)
let node = self.nodes(at: location).first
if node?.name == "BALL" {
currentScore += ballValue
player?.removeFromParent()
createBall(forTrack: track)
}
else {
let transition = SKTransition.fade(withDuration: 1)
gameScene = SKScene(fileNamed: "GameOverScene")
gameScene.scaleMode = .aspectFit
self.view?.presentScene(gameScene, transition: transition)
}
}
}