Game Development Community

[Solved] help to replace keyboard bind with button touch

by Ghonium · in iTorque 2D · 10/08/2011 (2:06 am) · 6 replies

hello
I built my game on tourque2d as i was learning how to use tgb
and now im trying to convert my game from torque2d to itorque2d

on PC to control my character I used behavior AsteroidsControl.cs

on that control when press keyboard arrow up it move the object forward

I want to do the same on iTorque2d is when image onTouchDown it move the object forward

so far I added my character to scene and applied asteroidControl to it, also I added Image to the corner of the screen as button and name the class upButton

please help how when touchDown the upButton is make the arrow key be true on asteroidControl.cs , and that to replace keyboard up with upButton image





#1
10/08/2011 (7:32 am)
you must look up and study mouse events. onMouseUp, acts like a touch down. With in that function, you can make your player move.

Also, having said that, if you have iTGB1.5, there is also onTouchDown... also various functions in the game.cs you can use to your advantage.


#2
10/08/2011 (8:27 am)
yes im trying to
function upButton::onTouchDown(%this, %touchID, %worldPos)
{
  // on touch the button image then make asteroidControlbahavior moveUp
}

function AsteroidsControlsBehavior::moveUp(%this, %val)
{
   %this.up = %val;
}

#3
10/09/2011 (4:10 am)
please help me in
convert asteroidControl.cs keyboard arrow up left right
with button for iPhone use
button touch make the character move up

I did the whole game on tgb now im trying to convert it to iTorque

in the current file when we press arrow up in keyboard it give true, I want just to change this arrow up key with button that when I touch on iphone it give true and same value of the arrow up
#4
10/09/2011 (8:08 am)
@Ghonium - Do you have the name of your player ship object set? If so, you can cause it to move like this:

function upButton::onTouchDown(%this, %touchID, %worldPos)
{
   playerShip.moveUp(1);
}

function upButton::onTouchUp(%this, %touchID, %worldPos)
{
   playerShip.moveUp(0);
}

There are a few other ways to get this done, such as using a behavior, but the above code should be enough to get you going. Look at the behaviors and code in the FeatureDemo project. There are a lot of examples there.
#5
10/09/2011 (9:08 am)
Thank you Michael I will try this soon once I get back to home :)
#6
10/10/2011 (8:29 am)
Thank you Michael it worked :)
function upButton::onTouchDown(%this, %touchID, %worldPos)
{
   echo("Finger " @ %touchID @ " touched the Cloud at " @ %worldPos);

   // If we are already dragging, do nothing
   // Otherwise, assign this finger to the cloud's touch ID
   if(upButton.touchID $= "")
   {
      upButton.touchID = %touchID;
      chicken.moveUp(1);

   }
}