Page 1 of 1

Acceleration code for Sonic

PostPosted: Wed Oct 13, 2010 9:42 am
by CrimsonTheDarkBat
Hey folks,

Has someone got the code to make the players speed start of slow and gradually get faster?
And also, a code to limit his speed to a certain level?

Thanks

Re: Acceleration code for Sonic

PostPosted: Wed Oct 13, 2010 5:49 pm
by TSO
I think more advanced ppl would do it on the fly.


Add event -> key down: right arrow -> script editor:
Code: Select all

//not sure how to declare variables but they might help in this bit of code...
//Also not sure if it redeclares them...
//Varibles can simply be replaced with set numbers.

int speed;
int topspeed=10;
int delay;

delay++;
if (delay=5)
{
  if (speed!=topspeed)
  {
  speed++;
  xvelocity+speed;
  }
  delay=0;
}


Something along those lines... would probably have to make a key up code to do the slow down with much less delay.
My syntax probably incorrect in a few places but the effect should achieve a 'sonic' style of speeding up I think and allow you to change speed limit...
just add some if statements to change to faster animations as speed increases...
probably make 'speed' a global variable for changing animations when changing direction and stuff in other key events.

Re: Acceleration code for Sonic

PostPosted: Wed Oct 13, 2010 8:18 pm
by lcl
TSO, that code won't work. :P
CrimsonTheDarkBat, here's code that works for moving on x axis. :D

Sonic - key down - key right - repeat: yes - script editor:
Code: Select all
xvelocity ++;  //increase xvelocity


Sonic - key down - key left - repeat: yes - script editor:
Code: Select all
xvelocity --;  //decrease xvelocity


Sonic - draw actor - script editor:
Code: Select all
if (xvelocity > 10) //10 is the limit value, you can change it to what you want
{
    xvelocity = 10;
}
if (xvelocity < - 10) //- 10 is the limit value, you can change it to what you want
{
    xvelocity = - 10;
}

xvelocity *= .95; //make it slow down if not pressing any key


:D

Re: Acceleration code for Sonic

PostPosted: Wed Oct 13, 2010 11:31 pm
by TSO
Yea i did say my syntax was off =P

Nice simple code.

Re: Acceleration code for Sonic

PostPosted: Fri Oct 29, 2010 11:13 pm
by CrimsonTheDarkBat
Thanks for the help guys!

But still in a pickle :/ TSO's code works to a certain extent, but Sonic accelerates too fast and stops too suddenly.
I've tried modifying the code in the way you said, but then Sonics overall speed decreases as a result.

Is there any other code to make Sonic accelerate slowly untill he gradually reaches a set speed? And then when the player releases the key, for him to slow down gradually untill he reaches a halt?

Thanks anybody who can help!

Re: Acceleration code for Sonic

PostPosted: Sat Oct 30, 2010 12:59 am
by zxcvbnm
Ok go in the game demo section and download my canabalt game. It that suits your need , just ask me for any modification.

Re: Acceleration code for Sonic

PostPosted: Sat Oct 30, 2010 2:35 am
by NERDnotGEEK
lcl's code works fine for what you want.
heres how id write it to give you let you have a little more controll later

Sonic - key down - key right - repeat: yes - script editor:
Code: Select all
run = 1;


Sonic - key up- key right - script editor:
Code: Select all
run = 0;


Sonic - draw actor - script editor:
Code: Select all
float accel = 0.1; // change to set the acceleration
float decel = 0.2; // change to set the deceleration
int maxspeed = 20; // change to set the max
if (run == 1)
{
    if (xvelocity < maxspeed)
    {
        xvelocity = xvelocity + accel;
    }
}
if (run == 0)
{
    if (xvelocity > 0)
    {
        xvelocity = xvelocity - decel;
    }
}


i think :) still getting to grips with ge myself

Re: Acceleration code for Sonic

PostPosted: Mon Nov 01, 2010 1:06 pm
by lcl
That's nice one. :D Though you still don't have code for moving left... :P

Re: Acceleration code for Sonic

PostPosted: Mon Nov 01, 2010 5:06 pm
by NERDnotGEEK
no one wants to be spoon fed, i just provided the general idea, obviously for a left, just add a run = 2, to keydown left, and run = 0 to keyup left, and then add this to the draw actor :)
Code: Select all
if (run == 2)
{
    if (xvelocity > -maxspeed)
    {
        xvelocity = xvelocity - accel;
    }
}

Re: Acceleration code for Sonic

PostPosted: Mon Nov 01, 2010 5:11 pm
by lcl
Yeah, I knew how to do that but crimsonthedarkbat maybe didnt. :D

Re: Acceleration code for Sonic

PostPosted: Mon Nov 01, 2010 5:11 pm
by lcl
EDIT: ehh... actually this is some weird double post... can't delete, so I decided to edit it... :P

Re: Acceleration code for Sonic

PostPosted: Mon Nov 01, 2010 5:56 pm
by NERDnotGEEK
ehh true :) but im sure he would have worked it out if he needed it :)