执行操作时如何阻止触摸

时间:2015-10-20 19:18:58

标签: ios swift sprite-kit touch skaction

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

玩家触摸屏幕并出现箭头

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

球员举起手指和箭射。

准确执行时效果很好。但是如果玩家不小心双击,游戏就会射出两支箭,并且比赛结束了99%。我想阻止这一点,但不知道该怎么做。我尝试了userInteractionEnabled,它阻止了交互,但在需要时却没有解锁。

我在屏幕上添加箭头如下:

import groovy.text.markup.MarkupTemplateEngine
import groovy.text.markup.TemplateConfiguration

String loadParameterizedTemplateByName(Map variables, String templateName) {
    def engine = new groovy.text.SimpleTemplateEngine()
    this.class.getResource(templateName).withReader { reader ->
        engine.createTemplate(reader).make(variables)
    }
}

def result = loadParameterizedTemplateByName('/mail.tpl', firstName:'Tim', someGuy:'StackOverflow', me:'smeeb')

assert result == 'Hello Tim,\n\nYou are awesome! StackOverflow even says so!\n\nSincerely,\nsmeeb'

1 个答案:

答案 0 :(得分:0)

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上。