Hello Guest

Author Topic: Using tk2dButton Events  (Read 15901 times)

sicbe

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Using tk2dButton Events
« on: May 04, 2012, 10:44:23 am »
Hello,

I've been trying all day to figure out how to use the button delegates...

I have created four tk2dButtons that I want to use as "controls" for the player.

Two buttons for moving left and right... They should keep a variable "moveLeft" or "moveRight" (in my movementController) at true when pressed  and revert the value to false when they are released.

Two other buttons for jump and attack... They should fire only once when pressed down. So when pressed down they should turn a variable "jump" or "attack" (in my movementController) to true (which is automatically turned to false at the end of the frame)...

How do I go about creating this using tk2dButtons? Is it even possible? Also I'm using Javascript and the reference is rather unclear about this... :-).

Thanks...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using tk2dButton Events
« Reply #1 on: May 04, 2012, 12:12:59 pm »
I'm assuming you've already run the "Set up for JavaScript step".

Here is a simple monobehaviour which you attach to the button object - it adds a listener to the ButtonDown event. You can do the same for all the other events thrown from there.

Code: [Select]
#pragma strict

var button : tk2dButton;
button = GetComponent(tk2dButton);


function Down()
{
Debug.Log("Down");
}

function Start () {
button.ButtonDownEvent += Down;
}

function Update () {

}

sicbe

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Using tk2dButton Events
« Reply #2 on: May 04, 2012, 01:43:25 pm »
Alright... Thanks :). That makes sense... Tried it and it works...

But it only works for ButtonAutofireEvent, ButtonPressedEvent and ButtonUpEvent... I can't get the ButtonDownEvent to work like in your example...

I tried:
button.ButtonDownEvent += Down;
button.ButtonUpEvent += Up;

with just logging "up" and "down" in the functions... I only get "up" logs in my console and no "down" logs...

Strange?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using tk2dButton Events
« Reply #3 on: May 04, 2012, 02:19:38 pm »
I just tried that and it seems to work fine. Which version of 2d toolkit are you using? I'm testing this on 1.7final, some bug fix went in in 1.7beta2 to do with events not firing properly, perhaps that is causing the problem? If you dont wish to update everything, drop me a PM / email to support, and I'll give you just that file to update.


sicbe

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Using tk2dButton Events
« Reply #4 on: May 04, 2012, 02:57:59 pm »
Yes I just noticed that the version downloaded from the asset store is different so I downloaded the 1.7 final here on the forum and now it works... :).

Aubrey

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Using tk2dButton Events
« Reply #5 on: December 04, 2012, 02:55:23 pm »
Updating to latest got the events to work for me, also. But I'm having trouble when I have Time.timeScale = 0.0f;

The Down and Up events seem to both fire on release of the button, and only then if i don't move the mouse. This seems similar to the problem i was having before the update, but only for Time.timeScale = 0.0f;

Oh, and this is in Unity 3.5.6f4

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using tk2dButton Events
« Reply #6 on: December 04, 2012, 10:51:03 pm »
That is rather bizzare.
In 1.80 + patch3, I definitely can't reproduce this. I get it firing regardless of whether the mouse moved or not.
If you can reproduce this, please send a test case to support@unikronsoftware.com

Code

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Using tk2dButton Events
« Reply #7 on: April 30, 2013, 02:39:35 pm »
Hey Unikron, is the ButtonDownEvent supposed to run every frame as long as the button is pressed down? I figured it would only happen once, until you lift your finger, but mine continues to activate.

Code: [Select]
    CrossButton.ButtonDownEvent  += OnScreenJumpDown;

 public void OnScreenJumpDown(tk2dButton source)
    {
        Debug.Log("Keeping finger on button");
    }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using tk2dButton Events
« Reply #8 on: April 30, 2013, 03:23:22 pm »
It shouldn't. Try turning off the scale when the button is activated, and see if its happenning then. If the button was too close to the near clip plane, and the box after being scaled was clipping the view, the finger/mouse cursor wouldn't register touch any more...

Code

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Using tk2dButton Events
« Reply #9 on: May 01, 2013, 09:29:39 am »
Weird, today when I started unity and tried changing the scaling from 0.01 to 0 (which still gave me problems before) it worked as it should. The buttons wasn't close to the near plane, so I'm not even sure why it did as it did. Oh well works now :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using tk2dButton Events
« Reply #10 on: May 01, 2013, 10:01:28 am »
How bizarre. But I'm not gonna complain if it works :)

Code

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Using tk2dButton Events
« Reply #11 on: May 01, 2013, 10:52:18 am »
Yeah, Unity can be odd sometimes  ;D

Right now I'm having another problem, If the player keeps his finger on the left button, but then chooses to swipe/follow along the screen until he touches the right button, the right isn't activated. I need to lift my finger in order to touch another button. Is there anything I can change to not have this behavior? :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using tk2dButton Events
« Reply #12 on: May 01, 2013, 11:49:00 am »
Which button are you using (tk2dButton or the one from the UI)? Also, do you have multitouch enabled in the UIManager?

Code

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Using tk2dButton Events
« Reply #13 on: May 01, 2013, 11:53:46 am »
2dtkbutton, there's no ui manager for that right?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Using tk2dButton Events
« Reply #14 on: May 01, 2013, 12:10:30 pm »
No but that isn't exactly meant to handle multiple fingers. My suggestion was to try out the tk2d UI button to see if it does what you need it to - if it does, switching from tk2dButton to that is pretty simple.