Hello Guest

Author Topic: Multi-touch problem with move and jump  (Read 4250 times)

atfield

  • Newbie
  • *
  • Posts: 2
    • View Profile
Multi-touch problem with move and jump
« on: October 01, 2013, 08:26:24 am »
here my code
Code: [Select]
void Start () {
uiItem = gameObject.GetComponent<tk2dUIItem>();
    if(uiItem){
uiItem.OnDownUIItem += Downed;
uiItem.OnReleaseUIItem += Released;
}
}


void OnDisable(){
UnsubscribeEvent();
}

void OnDestroy(){
UnsubscribeEvent();
}

void UnsubscribeEvent(){
// uiItem.OnClickUIItem -= Clicked;
if(uiItem){
uiItem.OnDownUIItem -= Downed;
uiItem.OnReleaseUIItem -= Released;
}
}



void Downed(tk2dUIItem clickedUIItem)
{
switch(name){
case "btnMoveLeft":
Player.isMoveL = true;
break;
case "btnMoveRight":
Player.isMoveR = true;
break;
}
}
void Released(tk2dUIItem clickedUIItem)
{
switch(name){
case "btnMoveLeft":
Player.isMoveL = false;
break;
case "btnMoveRight":
Player.isMoveR = false;
break;
case "btnJump":
GameObject.Find("man").GetComponent<Player>().JumpButton(true);
break;
}
}

look,while the ismoveL or ismoveR is true ,the player should move on x axis.
Even if when jump.
But unfortunatelly things happened.
After I press down the jump button when I am holding the move button,it also receives the other button's release event.Which means,I press jump,the moveL and moveR both become false.
So I can jump only on the origin place.

That's not right.
So any idea helping me to deal with such operation which can be very frequently used for a run-jump action game.
Thank you very much.
« Last Edit: October 01, 2013, 08:35:05 am by atfield »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Multi-touch problem with move and jump
« Reply #1 on: October 01, 2013, 10:59:08 am »
You can definitely do this. I don't know whats wrong with your script - I dont know how you have attached that in the scene, and I'm not sure why you're constantly binding and unbinding the events - you can do this a lot easier with one script and just filter the events from the script itself. Here's a working example. Make sure you have a tk2dUIManager with multitouch enabled for multitouch to work properly.

Code: [Select]
using UnityEngine;
using System.Collections;

public class ButtonTest : MonoBehaviour {

public tk2dUIItem btnLeft, btnRight, btnJump;

bool left = false, right = false, jump = false;

void OnEnable() {
btnLeft.OnDownUIItem += ButtonDown; btnRight.OnDownUIItem += ButtonDown; btnJump.OnDownUIItem += ButtonDown;
btnLeft.OnReleaseUIItem += ButtonRelease; btnRight.OnReleaseUIItem += ButtonRelease; btnJump.OnReleaseUIItem += ButtonRelease;
}

void OnDisable() {
btnLeft.OnDownUIItem -= ButtonDown; btnRight.OnDownUIItem -= ButtonDown; btnJump.OnDownUIItem -= ButtonDown;
btnLeft.OnReleaseUIItem -= ButtonRelease; btnRight.OnReleaseUIItem -= ButtonRelease; btnJump.OnReleaseUIItem -= ButtonRelease;
}

void ButtonDown(tk2dUIItem item) {
HandleButton(item.name, true);
}

void ButtonRelease(tk2dUIItem item) {
HandleButton(item.name, false);
}

void HandleButton(string name, bool val) {
switch (name) {
case "left": left = val; break;
case "right": right = val; break;
case "jump": jump = val; break;
}
}

void OnGUI() {
GUILayout.Label(string.Format("LEFT: {0} RIGHT: {1} JUMP: {2}", left, right, jump));
}
}