我在游戏中有一个Sprite节点,当被触摸时,它会执行被按下节点的操作,并在触摸开始时调用事件,然后在调用触摸结束事件时返回到正常大小。我的问题是当我按下节点然后将手指移到节点之外时,我将手指从屏幕上移开后并没有恢复到其原始大小。
我尝试在代码的“触摸移动”部分中使用多项操作,以尝试在保持触摸的同时将手指移出节点之外,但使其恢复到原始大小,但此操作不起作用。我的代码在下面
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)
}
}
在保持接触的同时将手指移到节点位置之外后,如何恢复到原始大小?
答案 0 :(得分:1)
在touchesBegan,touchesMoved,touchesEnded中,您始终引用当前节点。 let node = self.atPoint(touch.location(in: self))
为什么不保留对在touches中检测到的初始节点的引用,如果当前节点atPoint不等于初始节点,则开始对其进行缩放。我认为这可以解决您的问题。
编辑:
要在第一次触摸时捕获节点...
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并在需要时调整其大小