gpt4 book ai didi

ios - 当离开节点位置时,触摸事件不会使 spritenode 重置其大小

转载 作者:行者123 更新时间:2023-11-28 05:50:36 25 4
gpt4 key购买 nike

我在游戏中有一个 sprite 节点,当它被触摸时会执行节点被按下的 Action ,当触摸开始事件被调用时,然后在触摸结束事件被调用时恢复到正常大小。我的问题是,当我按下节点然后将手指移出节点时,手指离开屏幕后它不会恢复到原来的大小。

我尝试在代码的触摸移动部分使用多个东西来尝试在我按住触摸的同时将手指移出节点后让它恢复到原来的大小但它没有用.我的代码在下面

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

let touch: UITouch = touches.first!
let node = self.atPoint(touch.location(in: self))
let pushdown = SKAction.scale(to: 0.8, duration: 0.1)

if node == mainMenu.settingsButton {
node.run(pushdown)

} else if node == mainMenu.viewMapButton {
node.run(pushdown)

}else if node == mainMenu.shopButton {
node.run(pushdown)
}

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

let touch: UITouch = touches.first!
var location: CGPoint = touch.location(in: self)
let node: SKNode = atPoint(location)
let pushUp = SKAction.scale(to: 1.0, duration: 0.2)

if node != mainMenu.settingsButton {
//node.run(pushUp)

} else if touch.phase == .moved || touch.phase == .cancelled {

node.run(pushUp)

}

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

let touch: UITouch = touches.first!
let node = self.atPoint(touch.location(in: self))
let pushUp = SKAction.scale(to: 1.0, duration: 0.2)

if node == mainMenu.settingsButton {
node.run(pushUp)
//Run Sound Here

let scene = SettingsMenu(fileNamed:"SettingsMenu")
scene?.scaleMode = .aspectFill
//scene?.backgroundColor = UIColor.lightGray
let transition = SKTransition.crossFade(withDuration: 0.5)
self.scene?.view?.presentScene(scene!, transition: transition)

} else if node == mainMenu.viewMapButton {
node.run(pushUp)
}
}

在按住触摸键的同时将手指移出节点位置后,如何让它恢复到原始大小?

最佳答案

在 touchesBegan、touchesMoved、touchesEnded 中,您始终指的是当前节点所在的点。 让 node = self.atPoint(touch.location(in: self))如果当前节点 atPoint 不等于初始节点,为什么不保留对在 touchesBegan 中检测到的初始节点的引用以将其缩减。我想这会解决你的问题。

编辑:

在第一次触摸时捕获节点......

private var currentTouchedNode : SKNode?

override func touchesBegan(_ touches: Set, with event: UIEvent?) {

let touch: UITouch = touches.first!
currentTouchedNode = self.atPoint(touch.location(in: self))
let pushdown = SKAction.scale(to: 0.8, duration: 0.1)

if currentTouchedNode == mainMenu.settingsButton {
currentTouchedNode.run(pushdown)

} else if currentTouchedNode == mainMenu.viewMapButton {
currentTouchedNode.run(pushdown)

}else if currentTouchedNode == mainMenu.shopButton {
currentTouchedNode.run(pushdown)
}

}

在 touchesMoved 和 touchesEnded 中,您将比较当前节点是否等于 currentTouchedNode 并在需要时调整其大小

关于ios - 当离开节点位置时,触摸事件不会使 spritenode 重置其大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53128375/

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