2D Toolkit Forum

2D Toolkit => Support => Topic started by: sicbe on May 04, 2012, 10:44:23 am

Title: Using tk2dButton Events
Post by: sicbe 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...
Title: Re: Using tk2dButton Events
Post by: unikronsoftware 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 () {

}
Title: Re: Using tk2dButton Events
Post by: sicbe 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?
Title: Re: Using tk2dButton Events
Post by: unikronsoftware 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.

Title: Re: Using tk2dButton Events
Post by: sicbe 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... :).
Title: Re: Using tk2dButton Events
Post by: Aubrey 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
Title: Re: Using tk2dButton Events
Post by: unikronsoftware 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
Title: Re: Using tk2dButton Events
Post by: Code 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");
    }
Title: Re: Using tk2dButton Events
Post by: unikronsoftware 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...
Title: Re: Using tk2dButton Events
Post by: Code 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 :)
Title: Re: Using tk2dButton Events
Post by: unikronsoftware on May 01, 2013, 10:01:28 am
How bizarre. But I'm not gonna complain if it works :)
Title: Re: Using tk2dButton Events
Post by: Code 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? :)
Title: Re: Using tk2dButton Events
Post by: unikronsoftware 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?
Title: Re: Using tk2dButton Events
Post by: Code on May 01, 2013, 11:53:46 am
2dtkbutton, there's no ui manager for that right?
Title: Re: Using tk2dButton Events
Post by: unikronsoftware 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.
Title: Re: Using tk2dButton Events
Post by: Code on May 01, 2013, 12:12:28 pm
Okay I'll try do that :) Thanks!
Title: Re: Using tk2dButton Events
Post by: Code on May 03, 2013, 01:33:58 pm
Hey Unikron

I tried applying the UI buttons. I think that multi.touch is working more nicely now, but I still haven't got the desired effect.

What I want is when the user places his finger outside the button, then slides the finger along the screen and he gets above the button, the button should be activated. Is this possible? I couldn't find documentation for you ui beta, so I guess you haven't done one yet  :P
Title: Re: Using tk2dButton Events
Post by: unikronsoftware on May 03, 2013, 11:08:37 pm
You could possibly modify the code to do it, but it isn't designed to do that at all.
The way both button clicks are initiated is by pressing in it.