【问题标题】:How to block touches began while action is executed如何在执行操作时开始阻止触摸
【发布时间】:2015-10-20 19:18:58
【问题描述】:

我有一个具有这种机制的游戏:

玩家触摸屏幕并出现箭头

玩家拖动手指,改变箭头方向

玩家抬起手指并射箭。

准确执行时效果很好。但如果玩家不小心双击,游戏会射出两支箭,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))
    }
}

【问题讨论】:

  • 我会有一系列SKActions 来设置一个变量(类似于addArrowActionRunning),并且(我认为你可以)使用SKActions 来设置一个变量。所以它将addArrowActionRunning 设置为true,调用moveTo,然后将addArrowActionRunning 设置为false。也许这会有所帮助。
  • @Gliderman 你的意思是在addArrow 函数里面做?然后开始联系addArrow,仅当addArrowActionRunning == false?
  • 我看不出它有什么帮助

标签: ios swift sprite-kit touch skaction


【解决方案1】:

touchesBegan 方法中,将if 语句更改为如下所示:

if !GameOver && !arrow.hasActions() {
    runAction(SKAction.runBlock(addArrow))
}

这将检查您的全局 SKSpriteNode arrow 以查看是否已经有正在运行的操作。然后,如果没有,它将运行新的操作。如果您对此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。

【讨论】:

  • 我对@9​​87654334@ 的事情有点困惑。我应该把.actionForKey放在哪里?据我了解,它是一个只读属性,允许我阻止/取消阻止一个操作而不是全部?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多