Hello Guest

Author Topic: Movement of Sprite Restricted by ButtonUpEvent  (Read 4709 times)

amit.r007

  • Newbie
  • *
  • Posts: 4
    • View Profile
Movement of Sprite Restricted by ButtonUpEvent
« on: June 11, 2014, 06:10:46 am »
Hi Unikron,

I am making a 2D game in Unity, I have a sprite and two buttons left and right to the sprite in respective direction.

My logic goes this way, on ButtonDown event I move sprite and on ButtonUp I stop moving.  I am able to move the sprite in both directions on buttondown event, but when I release one button and suddenly press second button the movement of sprite stops and I have to again press that button to start moving.

Here is my code,
Code: [Select]
void Start(){
      GameObject.Find ("Btn_Left").GetComponent<tk2dButton> ().ButtonDownEvent += MoveLeft;
      GameObject.Find ("Btn_Left").GetComponent<tk2dButton> ().ButtonUpEvent += StopMoving;
      GameObject.Find ("Btn_Right").GetComponent<tk2dButton> ().ButtonDownEvent += MoveRight;
      GameObject.Find ("Btn_Right").GetComponent<tk2dButton> ().ButtonUpEvent += StopMoving;
}

void Update(){
      Sprite.transform.Translate(IPDir *0.1f,0,0);
}

void MoveLeft (tk2dButton source)
{
Debug.Log("Left is Pressed");
IPDir = -1;
}

void MoveRight (tk2dButton source)
{
IPDir = 1;
}

void StopMoving (tk2dButton source)
{
               IPDir = 0;
}

Everything works great except for that part when I am moving in right and suddenly I release right and press left and viceversa, the sprite stops moving. I had enabled multitouch option in editor, multitouch works great, but when dont know why it is not taking the next ButtonDown event when I am Releasing one button.

Help me regarding this, I searched on forums but could not get closer to my solution.

Regards,
Amit

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Movement of Sprite Restricted by ButtonUpEvent
« Reply #1 on: June 11, 2014, 11:13:36 am »
Print out the StopMoving and MoveRight events, I think they may be getting called in a different order than you expect. You should probably have a moveLeft and moveRight variable, then manage them independently. Add the values in Update to get the total sum movement so you wont rely on order of the variables getting updated.

amit.r007

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Movement of Sprite Restricted by ButtonUpEvent
« Reply #2 on: June 11, 2014, 04:19:59 pm »
I took two bool moveLeft and moveRight and handled the code accordingly in update. It worked great, but can you tell me what was the mistake in my previous code. Is it the sequence of events getting called that caused the issue, I want to get it cleared ... :D :D

Thanks,
Amit

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Movement of Sprite Restricted by ButtonUpEvent
« Reply #3 on: June 11, 2014, 04:20:55 pm »
The events dont fire in any particular order. It is pretty much arbitrary, so if Stop fires after moveright is set it will break.