Author Topic: Disabling JUMP-Button after Jumping  (Read 2415 times)

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Disabling JUMP-Button after Jumping
« on: May 26, 2013, 09:04:57 AM »
Hey there.

So I've got this Movement/Action-Script which is attached to all my four UIImage-Buttons. They are seperated in four different states and do their job well.

  1.     using UnityEngine;
  2.     using System.Collections;
  3.      
  4. public class TestButton : MonoBehaviour
  5. {
  6.         public ButtonType type;
  7.         public NGUIInput input;
  8.         public RaycastCharacterController controller;
  9.         public tk2dAnimatedSprite playerSprite;
  10.         public SpriteAnimator animator;
  11.         public GameObject weapon;
  12.         private bool attacking = false;
  13.        
  14.         void HitCompleteDelegate (tk2dAnimatedSprite sprite, int clipId)
  15.         {
  16.                
  17.                 if (!playerSprite.IsPlaying ("attack")) {
  18.                         weapon.active = false;
  19.                 }
  20.                                
  21.                 if (!attacking) {
  22.                         CheckDirectionIdle ();
  23.                         playerSprite.Play ("idle");
  24.                 }
  25.                
  26.                 if (animator.jumping) {
  27.                         playerSprite.Play ("jump");
  28.                 }
  29.                
  30.                 if (animator.walking) {
  31.                         playerSprite.Play ("walk");
  32.                 }
  33.         }
  34.        
  35.         void Start ()
  36.         {
  37.                 weapon.active = false;
  38.         }
  39.        
  40.         void Update ()
  41.         {
  42.                 if (playerSprite.IsPlaying ("jump"))
  43.                         CheckDirectionIdle ();
  44.         }
  45.        
  46.         public void OnPress (bool pressed)
  47.         {
  48.                 if (pressed) {
  49.                         switch (type) {
  50.                                
  51.                         case ButtonType.LEFT :
  52.                                 input.SetX (-1f);
  53.                                 weapon.active = false;
  54.                                 playerSprite.animationCompleteDelegate = HitCompleteDelegate;
  55.                                
  56.                                 if (playerSprite.IsPlaying ("attack"))
  57.                                         playerSprite.Stop ();
  58.                                 animator.walking = true;
  59.  
  60.                                 break;
  61.                
  62.                         case ButtonType.RIGHT :
  63.                                 input.SetX (1f);
  64.                                 weapon.active = false;
  65.  
  66.                                 playerSprite.animationCompleteDelegate = HitCompleteDelegate;
  67.                                
  68.                                 if (playerSprite.IsPlaying ("attack"))
  69.                                         playerSprite.Stop ();
  70.                                 animator.walking = true;
  71.                                 break;
  72.                
  73.                        
  74.                    
  75.                         case ButtonType.JUMP :
  76.                                 input.Jump (true);
  77.                                 weapon.active = false;
  78.                                
  79.                                 playerSprite.animationCompleteDelegate = null;                         
  80.                                 break;
  81.                                
  82.                                
  83.                                
  84.                         case ButtonType.ATTACK :
  85.                                
  86.                                 if (!playerSprite.IsPlaying ("attack")) {
  87.                                         playerSprite.Play ("attack");
  88.                                         weapon.active = true;
  89.                                         attacking = true;
  90.  
  91.                                         playerSprite.animationCompleteDelegate = HitCompleteDelegate;
  92.                                         CheckDirectionAttack ();
  93.                                 }
  94.                                
  95.                                 attacking = false;
  96.  
  97.                                 break;
  98.                         }
  99.                 } else {
  100.                         switch (type) {
  101.                         case ButtonType.LEFT :
  102.                                 input.SetX (0.0f);
  103.                                 weapon.active = false;
  104.  
  105.                                 break;
  106.                         case ButtonType.RIGHT :
  107.                                 input.SetX (0.0f);
  108.                                 weapon.active = false;
  109.  
  110.                                 break;
  111.                         case ButtonType.JUMP :
  112.                                 input.Jump (false);
  113.                                 weapon.active = false;
  114.                                
  115.                                 break;
  116.                         case ButtonType.ATTACK :
  117.                                 input.SetX (input.x);
  118.  
  119.                                 weapon.active = false;
  120.                                
  121.                                 break;
  122.                         }
  123.                 }
  124.         }
  125.        
  126.    
  127. public enum ButtonType
  128. {
  129.         LEFT,
  130.         RIGHT,
  131.         JUMP,
  132.         ATTACK,
  133. };

The Problem is the JUMP-Part.

When I press that Button the Character is jumping. Fine. But when I press and HOLD it, the Character is jumping all the time with no change in other States, and of course the Animation is "stuck" in the last frame of the Jump-Animation. What do I want to do:

Is there a way to stop or disable the input of nGUI, meaning: the Player presses and HOLDS the Button, the Character jumps BUT stays grounded after that and the Player has to release the Button and press again.

Thanks in Advance for the Answers :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Disabling JUMP-Button after Jumping
« Reply #1 on: May 26, 2013, 12:53:14 PM »
Instead of trying to bend NGUI to do this, change your own code. Once you jump, there is obviously going to be a delay until input should be usable again, so do just that -- add some value like so:
  1. mNextUpdateTime = Time.time + 3f;
And in your OnPress function check this value:
  1. if (mNextUpdateTime > Time.time) return;