swift - 如何将 UItapGesture 识别器指定为按钮的 CGpoint

标签 swift timer action uigesturerecognizer

我正在制作一个游戏广告,但我很难制作一个跳转按钮。我已经创建了跳上跳下的 SKaction 序列,它的工作原理非常完美。

func JumpArrow () {
    self.addChild(jumpArrow)
    jumpArrow.position = CGPointMake(60, 145)
    jumpArrow.xScale = 1
    jumpArrow.yScale = 1
}

func heroJumpMovement () {
    let heroJumpAction = SKAction.moveToY(hero.position.y + 85, 
    duration: 0.5)
    let heroFallAction = SKAction.moveToY(hero.position.y , duration:
    0.5)

    let jumpWait:SKAction = SKAction.waitForDuration(CFTimeInterval(1)) 

    let heroMovementSequence:SKAction = 
    SKAction.sequence([heroJumpAction, heroFallAction ,jumpWait])
    hero.runAction(heroMovementSequence)

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

    for touch in touches {

        let location = touch.locationInNode(self)
        let node = nodeAtPoint(location)

if node == jumpArrow {

           heroJumpMovement()
 }

但是,我有一个问题。如果您快速点击按钮,播放器将飞出屏幕。我希望我可以创建一个 UItapGestureRecognizer 并为点击创建一个延迟,这样你就不能每秒点击按钮 2-4 次,你只能点击一次。如果这是错误的方法,请告诉我

最佳答案

添加延迟是错误的做法。

相反,在您的 touchesBegan 函数中,在您调用 heroJumpMovement() 之前,您应该检查您的玩家是否在地面上.

另一种方法是检查最后一次跳转 SKActionSequence 是否完成

要执行上述操作,您会得到类似这样的东西(未检查代码):

var canJump = true; // Variable will be true if we can jump

func JumpArrow () {
    self.addChild(jumpArrow)
    jumpArrow.position = CGPointMake(60, 145)
    jumpArrow.xScale = 1
    jumpArrow.yScale = 1
}

func heroJumpMovement () {
    let heroJumpAction = SKAction.moveToY(hero.position.y + 85, 
    duration: 0.5)
    let heroFallAction = SKAction.moveToY(hero.position.y , duration:
    0.5)

    let jumpWait:SKAction = SKAction.waitForDuration(CFTimeInterval(1)) 

    let heroMovementSequence:SKAction = 
    SKAction.sequence([heroJumpAction, heroFallAction ,jumpWait])
canJump = false; // We are about to jump so set this to false
    hero.runAction(heroMovementSequence, completion: {canJump = true;}) // Set the canJump variable back to true after we have landed

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

    for touch in touches {

        let location = touch.locationInNode(self)
        let node = nodeAtPoint(location)

if node == jumpArrow {
          if (canJump) { // Make sure we are allowed to jump
           heroJumpMovement()
          }
 }

注意 canJump 变量。

关于swift - 如何将 UItapGesture 识别器指定为按钮的 CGpoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36875236/

相关文章:

ios - AWS SDK swift com.amazonaws.AWSSNSErrorDomain Code=0 "(null)"

android - 如何将数据从本地服务发送到 ANDROID 中的 Activity

java - Android 定时器不精确

javascript - 使用javascript访问不同页面上的表单元素

javascript - 用于更改字体和字体大小的 Photoshop 脚本

ios - 如何使 Cocoa Touch 框架在应用程序中保持最新

ios - 在 iOS 应用程序中控制 Spotify 背景音乐

xcode - 在 xCode/使用 Swift 上更改 UITabBar 标题

c# - 如何在 Action 中使用可选参数?

c# - session 超时与定时器