如何在执行操作时阻止触摸开始
How to block touches began while action is executed
我的游戏有这样的机制:
玩家触摸屏幕并出现箭头
玩家拖动手指,改变箭头方向
玩家抬起手指,箭射出。
准确执行时效果很好。但如果玩家不小心双击,游戏会射出两支箭,99% 的游戏就结束了。我想阻止这种情况,但不知道该怎么做。我尝试了 userInteractionEnabled,它会阻止交互但在需要时不会取消阻止。
我像这样在屏幕上添加箭头:
func addArrow() {
arrow = SKSpriteNode(imageNamed: "red")
arrow.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) - self.arrow.size.height)
arrow.zPosition = 1
addChild(arrow)
arrowAppear.play()
let moveToShoot = SKAction.moveTo(CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMinY(self.frame) + self.arrow.size.height + 50), duration: 0.2)
arrow.runAction(moveToShoot)
SKActionTimingMode.EaseOut
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if !GameOver {
runAction(SKAction.runBlock(addArrow))
}
}
在 touchesBegan
方法中,将 if
语句更改为如下所示:
if !GameOver && !arrow.hasActions() {
runAction(SKAction.runBlock(addArrow))
}
这将检查您的全局 SKSpriteNode
arrow
以查看是否已经有 运行ning 操作。然后,如果没有,它将 运行 新操作。如果您对此 arrow
有多个操作,您可能希望调用 arrow.actionForKey("your key")
以获取操作(如果有),然后检查它是否存在。并输入 arrow.runAction(moveToShoot, withKey: "your key")
而不是 arrow.runAction(moveToShoot)
.
编辑 例如:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if !GameOver {
if let action = arrow.actionForKey("your key") {
// Has the action already, do not add an action
} else {
// Does not have the action, add it
runAction(SKAction.runBlock(addArrow))
}
}
}
然后在您的 addArrow
函数中:
func addArrow() {
arrow = SKSpriteNode(imageNamed: "red")
arrow.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) - self.arrow.size.height)
arrow.zPosition = 1
addChild(arrow)
arrowAppear.play()
let moveToShoot = SKAction.moveTo(CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMinY(self.frame) + self.arrow.size.height + 50), duration: 0.2)
// This is where you run your action with the key
arrow.runAction(moveToShoot, withKey: "your key")
SKActionTimingMode.EaseOut
}
我不确定 SKActionTimingMode.EaseOut
在做什么,但我只是想指出这一点。我认为您需要将 放到 SKAction 中。
我的游戏有这样的机制:
玩家触摸屏幕并出现箭头
玩家拖动手指,改变箭头方向
玩家抬起手指,箭射出。
准确执行时效果很好。但如果玩家不小心双击,游戏会射出两支箭,99% 的游戏就结束了。我想阻止这种情况,但不知道该怎么做。我尝试了 userInteractionEnabled,它会阻止交互但在需要时不会取消阻止。
我像这样在屏幕上添加箭头:
func addArrow() {
arrow = SKSpriteNode(imageNamed: "red")
arrow.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) - self.arrow.size.height)
arrow.zPosition = 1
addChild(arrow)
arrowAppear.play()
let moveToShoot = SKAction.moveTo(CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMinY(self.frame) + self.arrow.size.height + 50), duration: 0.2)
arrow.runAction(moveToShoot)
SKActionTimingMode.EaseOut
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if !GameOver {
runAction(SKAction.runBlock(addArrow))
}
}
在 touchesBegan
方法中,将 if
语句更改为如下所示:
if !GameOver && !arrow.hasActions() {
runAction(SKAction.runBlock(addArrow))
}
这将检查您的全局 SKSpriteNode
arrow
以查看是否已经有 运行ning 操作。然后,如果没有,它将 运行 新操作。如果您对此 arrow
有多个操作,您可能希望调用 arrow.actionForKey("your key")
以获取操作(如果有),然后检查它是否存在。并输入 arrow.runAction(moveToShoot, withKey: "your key")
而不是 arrow.runAction(moveToShoot)
.
编辑 例如:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if !GameOver {
if let action = arrow.actionForKey("your key") {
// Has the action already, do not add an action
} else {
// Does not have the action, add it
runAction(SKAction.runBlock(addArrow))
}
}
}
然后在您的 addArrow
函数中:
func addArrow() {
arrow = SKSpriteNode(imageNamed: "red")
arrow.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) - self.arrow.size.height)
arrow.zPosition = 1
addChild(arrow)
arrowAppear.play()
let moveToShoot = SKAction.moveTo(CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMinY(self.frame) + self.arrow.size.height + 50), duration: 0.2)
// This is where you run your action with the key
arrow.runAction(moveToShoot, withKey: "your key")
SKActionTimingMode.EaseOut
}
我不确定 SKActionTimingMode.EaseOut
在做什么,但我只是想指出这一点。我认为您需要将 放到 SKAction 中。