gpt4 book ai didi

ios - Swift 和 Sprite 套件接触不正确

转载 作者:行者123 更新时间:2023-11-28 12:09:41 25 4
gpt4 key购买 nike

我正在使用 Sprite Kit 制作一款游戏,其中用户的点击球会穿过屏幕。球每 1 秒产生一次。但是,如果生成了两个球并且用户点击了第一个球,则只有第二个球(以及之后生成的任何球)将被删除/记录,而不是用户实际点击的那个。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node = self.nodes(at: location).first

if node?.name == "BALL" {
currentScore += ballValue
player?.removeFromParent()
}
else {
gameOver()
}
}

}

override func didMove(to view: SKView) {
setupTracks()
createHUD()

self.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
self.createBall(forTrack: self.track)
}, SKAction.wait(forDuration: 2)])))

}

func createBall(forTrack track: Int) {

setLevel()

player?.name = "BALL"
player?.size = CGSize(width: 100, height: 100)
ballValue = 1

let ballPosition = trackArray?[track].position

player?.position = CGPoint(x: (ballPosition?.x)!, y: (ballPosition?.y)!)
player?.position.y = (ballPosition?.y)!
player?.zPosition = 1

if ballDirection == "right" {
player?.position.x = 0
moveRight()
}
else {
player?.position.x = (self.view?.frame.size.height)!
moveLeft()
}

}

最佳答案

我很确定这是因为你这样做了:

player?.removeFromParent()

无论触摸哪个 Sprite ,player 总是最后生成的 Sprite 。您已经将被触摸的节点分配给 node,所以我认为您需要执行以下操作:

node.removeFromParent()

相反。

关于ios - Swift 和 Sprite 套件接触不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48724737/

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