Since I got so many request about this stuff, so here I make it.
ON-SCREEN CONTROL FOR PLATFORMER GAME
First of all, create the needed actors:
1. ButtonLeft
2. ButtonRight
3. Button Jump
Don't forget to set those button's ZDepth to the highest value. And the transparency to be about 30% (optional)
4. Platform
5. Background
6. Player
For easier animation management for the player, I named his animations with format "Name-Action-Direction" as follows:
- "PlayerStopLeft"
- "PlayerStopRight"
- "PlayerRunLeft"
- "PlayerRunRight"
- "PlayerJumpLeft"
- "PlayerJumpRight"
Now, let's move to the coding step:
1. Prepare the needed variables on Global code
- Code: Select all
int Action; //0: Stop, 1: Run
int Direction; //-1: Left, 0: None, 1: Right
int Jump; //0: On the ground, 1: Jumping
char PlayerAction[3][16]={"Stop","Run"};
char PlayerDirection[4][16]={"Left","None","Right"};
2. Set the Player basic code:
- Player > Collision > Any Side; Repeat: Yes > Platform:
- Code: Select all
PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1.000000, 1.000000, 0.000000, 0.000000);
- Player > Collision > Top Side; Repeat: Yes > Platform:
- Code: Select all
Jump=0; //Grounding
- Player > Create Actor:
- Code: Select all
//Initial value
Action=0; //Stop
Direction=1; //Facing Right
- Player > Draw Actor:
- Code: Select all
char tmp[32];
yvelocity++; //gravity effect
x+=Action*Direction*5; //5 is the walking speed
if(Jump==0) //if on the ground
{
//Get the player animation name based on it's condition (Name+Action+Direction)
//and store it to 'tmp' variable
sprintf(tmp,"Briver%s%s",PlayerAction[Action],PlayerDirection[Direction+1]);
//change the animation as the stored value
ChangeAnimation("Event Actor", tmp, NO_CHANGE);
}
if(Jump==1) //if jumping
{
sprintf(tmp,"BriverJump%s",PlayerDirection[Direction+1]);
ChangeAnimation("Event Actor", tmp, NO_CHANGE);
}
3. Now, setup the control. This is the key feature. While in normal control (without using on-screen key) you set the control mostly in "Player > Key Down" and "Player > Key Up"... in on-screen control, we set the player control in "Button > Mouse Click" and "Button > Mouse Down".
- ButtonLeft > Mouse Button Down > (Left Click):
- Code: Select all
Action=1; //Walk
Direction=-1; //Facing Left
- ButtonLeft > Mouse Button Up > (Left Click):
- Code: Select all
Action=0; //Stop
- ButtonRight > Mouse Button Down > (Left Click):
- Code: Select all
Action=1; //Walk
Direction=1; //Facing Right
- ButtonRight > Mouse Button Up > (Left Click):
- Code: Select all
Action=0; //Stop
- ButtonJump > Mouse Button Down > (Left Click):
- Code: Select all
if(Jump==0) //if on the ground
{
Player.yvelocity=-15;
Jump=1; //Jump
}
That is all the basic.
You can add more tweaks you want.
Let me know if you have any question.
Good luck
Here's the full demo: http://dl.dropbox.com/u/3997355/Demo/On ... ontrol.zip