Page 1 of 1

Jump, walk, stop buttons for iphone touch screen

PostPosted: Mon Jan 02, 2012 2:57 am
by raminkhan
Hi I was wondering to know how can I make a touch screen buttons for my character to walk, jump and stop based on a tab buttons for Iphone. can someone please guide me?

Re: Jump, walk, stop buttons for iphone touch screen

PostPosted: Mon Jan 02, 2012 4:36 am
by skydereign
Touch controls have to do entirely with mouse button down and up events. There are generally three types of buttons you would use for controlling your actor. Which ones you use depend on the function of your button. The first is the simplest, and its most common use is for jump. All you need is a mouse button down event.
jump_button -> Mouse Button Down (Left) -> Script Editor
Code: Select all
if(jump>0)
{
    player.yvelocity=-15; // notice it uses the player's name to set the player's yvelocity to -15
    ChangeAnimation("player", "jump", FORWARD);
    jump--;
}

Note for the above if you are dealing with a platformer where you can turn around, you'll need to use a direction variable of some sort to change the animation to jump_right or jump_left.

The second type of button is a button that while pressed keeps running an event. For instance if you are moving the player by using x+= instead of xvelocity=, you'll want to use this. It involves a variable (I suggest an actor variable as other buttons may have a similar variable) I tend to call it state or click_state. When you click the actor, you set the state to 1, and when you have a mouse button up event, you set it back to 0. This is useful because you can add in its draw actor event a way of knowing if the button is clicked.
right_button -> Mouse Button Down (Left) -> Script Editor
Code: Select all
state=1;

right_button -> Mouse Button Up (Left) -> Script Editor
Code: Select all
state=0;

right_button -> Draw Actor -> Script Editor
Code: Select all
if(state==1)
{
    player.x+=10;
    ChangeAnimation("player", "run_right", NO_CHANGE);
}

Note in this small example of the button type, I didn't include any anti-moonwalking system.

The last major type of 'button' is a joystick. This is a little more difficult, only because the multitouch is a little weird. But, it uses the same idea with the state variable as the previous button. In addition though, you use FollowMouse to make the actor follow the click, and when you let go, you stop it from following the mouse, and reset its position. The example given is something you might see in a shmup.
joystick -> Draw Actor -> Script Editor
Code: Select all
if(state==1)
{
    player.directional_velocity=10;
    player.angle=direction(normal_x, normal_y, x, y); // normal_x and normal_y need to be replaced with the normal x and y positions
    // normal meaning when not clicked
}