Another method with simpler code but a bit more complicated preparation:
PREPARATION
Give the player 3 run animations, same animation but with different fps. For example:
- PlayerRunLeft1 ~ 10 fps
- PlayerRunLeft2 ~ 20 fps
- PlayerRunLeft3 ~ 30 fps
- PlayerRunRight1 ~ 10 fps
- PlayerRunRight2 ~ 20 fps
- PlayerRunRight3 ~ 30 fps
And stop animation:
- PlayerStopLeft1 ~ 10 fps
- PlayerStopRight1 ~ 10fps
Make 4 integer variables via script editor:
- speed
- run
- direct
- stop
CODE
Player > KeyDown > Left > ScriptEditor:
- Code: Select all
run〓1;
stop〓0;
speed++;
direct〓-1;
Player > KeyDown > Right > ScriptEditor:
- Code: Select all
run〓1;
stop〓0;
speed++;
direct〓1;
Player > KeyUp > Any Key > ScriptEditor:
- Code: Select all
run〓0;
Player > DrawActor > ScriptEditor:
- Code: Select all
char str[32];
char directText[3][8]〓{"Left","None","Right"};
char stateText[2][8]〓{"Run","Stop"};
speed-〓(run〓〓0)*5; //if you release the button, reduce the speed bit by bit
speed〓max(0,min(speed,59)); //limit the speed value
x+〓speed/5*(stop〓〓0)*direct; //x movement decided by speed, stop and direct
sprintf(str,"Player%s%s%i",stateText[stop],directText[direct+1],speed/20+1); //get the animation name
ChangeAnimation("Event Actor", str, NO_CHANGE); //change the player animation
if(speed<〓0&&run〓〓0)stop〓1;