Hello Guest

Author Topic: Android touch input  (Read 4449 times)

blumpkinblake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Android touch input
« on: April 27, 2014, 11:56:58 pm »
I have been searching everywhere looking for handling touchscreen inputs with tk2d.

My current setup is a button that consists of tk2dSprite with a tk2dUiItem. I don't have a collider on it since my Sprite will interact with the button which I don't want it to do.

I have my buttons (left, right, jump, pause) all connected to my camera so they stay in the same place on the screen with player movement.

Here is some code that I have tried:

Code: [Select]
foreach (Touch touch in Input.touches)
        {
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
            {
                if (_leftTexture.IsPressed)
                {
                    Move_Left();
                }
                if (_rightTexture.IsPressed)
                {
                    Move_Right();
                }

                if (_jumpTexture.IsPressed)
                    Jump();
            }
        }

The above code is in a c# movement script that I have made.

My _leftTexture and so on are defined as a public tk2dUiItem above so that I can set the in Unity.

I have tried the SendMessage in the Tk2dUiItem preferences to activate the function I want to do with no avail.


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Android touch input
« Reply #1 on: April 28, 2014, 11:05:12 am »
If you want to use tk2dUIItem, you don't do this.

I recommend starting with a sample button to understand how this works. Here is an example on how to do this. http://2dtoolkit.com/docs/latest/ui/customizing_button.html
Once you have the button you can use the drag and drop method (Send Message Target) to link it up to your script.

If you want to create your own button from scratch, create a sprite, make sure it has a collider and add a tk2dUIItem script to it. This will behave exactly the same as the button you drag in, except it doesn't scale or play a sound when you click on it.

blumpkinblake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Android touch input
« Reply #2 on: April 28, 2014, 09:10:11 pm »
Perfect, it works!

The problem was: I was using a box collider 2d instead of a normal collider

Thankyou

blumpkinblake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Android touch input
« Reply #3 on: April 29, 2014, 02:58:15 am »
Now I have another issue,

There is only a sendmessage for on down, on up, on click, on release.

My current setup is a 2d game with a left button next to a right button

When I set on down to left_true and right_true respectively.
On up I set to left_false and right_false respectively

Code: [Select]
private void left_true()
    {
        left = true;
    }

    private void right_true()
    {
        right = true;
    }
   
    private void left_false()
    {
        left = false;
        Zero();
    }

    private void right_false()
    {
        right = false;
        Zero();
    }

In my fixed_update if left or right is true it will then move them

However, what I want is to be able to slide my finger from right to left and have it disable right and enable left.

The way it's working right now you have to let go of the screen and click on the next button.

I have a tk2duimanager with multitouch checked as well.

Is there a way to activate the onup by moving your finger off the button and activate the ondown for the other button without lifting your finger?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Android touch input
« Reply #4 on: April 29, 2014, 11:08:50 am »
No there isn't, its meant to behave like a button. This has been requested before, but not high enough on priority list due to lack of interest.
https://trello.com/c/qdYL5F4s/55-touch-and-drag-to-trigger-ondown-for-example-arrow-buttons-for-a-on-screen-virtual-controller
https://trello.com/c/GtTCPbJq/45-hover-support-with-multitouch-finger-dragged-into-a-control-without-being-tapped

You can implement it yourself on a custom control - i.e. you treat the 2 buttons as 1 tk2dUIItem button and locally detect which internal button the user has clicked on in your event handler.