2016-04-26 2 views
1

ゲーム広告を作成しています。ジャンプボタンを作成するのが苦労しています。私はジャンプアップを作成し、ここで完璧に働くSKactionシーケンスは、どのように動作するのか落ちる。ボタンのCGポイントにUItapGestureレコグナイザを指定する方法

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を作成してタップの遅延を作成して、1秒間に2〜4回タップできないようにして、1回しかタップできないことを願っています。これが間違った方法であれば教えてください

答えて

1

遅延を追加すると間違った方法になります。

代わりに、あなたのtouchesBegan機能では、あなたのプレイヤーが地上にあるかどうかを確認するためにheroJumpMovement()あなたべきチェックを呼び出す前に。

もう1つの方法は、最後のジャンプ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変数を。

+0

おかげで、その方法は完璧に機能しました – gkolman

関連する問題