gpt4 book ai didi

ios - Sprite 节点位置未通过触摸更新?

转载 作者:行者123 更新时间:2023-11-29 05:41:40 25 4
gpt4 key购买 nike

本质上,我想要的是当我触摸一个节点时,我希望能够将它移动到屏幕上。问题是,每当我移动手指太快时,节点就会停止跟随它。

特别是我尝试使用的 spriteNode 具有物理体和动画纹理,因此我尝试使用完全普通的 spriteNode 执行相同的代码,并且遇到了同样的问题。

我这里的代码非常简单,所以我不确定这是否是我编写的问题,或者只是一个我无法修复的滞后问题。在touchesBegan、touchesMoved 和touchesEnded 中它也基本相同

for touch in touches {

let pos = touch.location(in: self)
let node = self.atPoint(pos)

if node.name == "activeRedBomb"{
node.position = pos
}

if node.name == "activeBlackBomb"{
node.position = pos
}


if node.name == "test"{
node.position.x = pos.x
node.position.y = pos.y
}


}

最佳答案

发生的情况是,如果您移动手指太快,那么在某个时刻,触摸位置将不再位于 Sprite 上,因此您移动节点的代码将不会触发。

你需要做的是在touchesBegan()中设置一个标志来指示这个 Sprite 被触摸,在touchesMoved()中将 Sprite 移动到触摸的位置> 如果设置了标志,然后在 touchesEnded() 中重置标志。

以下是您需要为此添加的大致内容:

import SpriteKit

class GameScene: SKScene {

var bombIsTouched = false

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if activeRedBomb.contains(touch.location(in: self)) {
bombIsTouched = true
}
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if bombIsTouched {
activeRedBomb.position = (touches.first?.location(in: self))!
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if bombIsTouched {
bombIsTouched = false
}
}

关于ios - Sprite 节点位置未通过触摸更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56485910/

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