Jump to content


Photo

How do i make my character run?


  • Please log in to reply
1 reply to this topic

#1 FinalEdition

FinalEdition

    GMC Member

  • New Member
  • 44 posts

Posted 30 December 2010 - 01:58 AM

I would like to make it so when left shift is held my obj_player will run. and also when it runs it will jump higher.

Also if its not too much I would like if running starts off slower and gradually gains speed.

under player create event:
//Set up some variables for certain speeds
walksp=4; //Walking speed (4 pixels per frame)
jumpsp=-14; //Jumping speed (negative because it goes up)
grav=1; //Gravity strength (usually a small value)

//These variables are actually used to move the player
hsp=0; //Horizontal speed
vsp=0; //Vertical speed
ground=1; //Whether the player is on the ground

under player step event:
//Keyboard constants
//We use these so we can just type the variables
//instead of "keyboard_check(*)"
KEY_RIGHT=keyboard_check(vk_right);
KEY_LEFT=keyboard_check(vk_left);
KEY_JUMP=keyboard_check_pressed(vk_up);


//This one's for variable jump height:
KEY_FALL=keyboard_check_released(vk_up);

//Now check if the player pressed left or right and move
if (KEY_RIGHT)
{
    hsp=walksp; //Walk right
    image_xscale=1; //Face right
}

if (KEY_LEFT)
{
    hsp=-walksp; //Walk left
    image_xscale=-1; //Face left
}

//Stop moving when no keys are pressed
if (!KEY_RIGHT and !KEY_LEFT) hsp=0;

//Make sure we don't hit a wall
if (place_meeting(x+hsp,y,objBlock))
{
    //Move until contact with the wall
    if (hsp!=0)
        while (!place_meeting(x+sign(hsp),y,objBlock))
            x+=sign(hsp);
    hsp=0;
}

//Because we don't use hspeed, we got to move ourselves
x+=hsp;

//Now for vertical motion (jumping and falling)

//Is the player in the air?
if (place_meeting(x,y+1,objBlock)) grounded=1;
else grounded=0;

//Jump with the up key when on the ground
if (KEY_JUMP and grounded)
{
    vsp=jumpsp;
}

//If we're in air moving up and jump key is released, we remove
//upward motion (so we fall and get variable jump height)
if (KEY_FALL and !grounded and vsp<-1) vsp=-1;

//Fall with gravity
if (!grounded) vsp+=grav;

//Now it's more complicated.
//When hitting the ceiling, vertical speed must stop.
//The if statement says, "if we hit the ceiling and are moving up"


if (place_meeting(x,y+vsp,objBlock) && vsp<0)
{
    //We must move up until contact with the ceiling
    while (!place_meeting(x,y+sign(vsp),objBlock)) y+=sign(vsp);
    vsp=0;
}

//But if we are moving down and hit the floor, we have to land
if (place_meeting(x,y+vsp,objBlock) and vsp>0)
{
    //Move so we hit the ground
    var cc;
    cc=vsp+1; //A counter, so we don't get an infinite loop

     //Move down until we hit the floor
    while (!place_meeting(x,y+1,objBlock) and cc>=0) y+=1;

     //Now ground the player
    grounded=1;

    vsp=0;
}

//Again, we're not using vspeed, so we have to move ourselves
y+=vsp;

//Animation - check what our sprite's state is and set accordingly
if (grounded) //If we're grounded
{
    if (hsp==0) //Then if we're not moving, change to stand sprite
    {
        sprite_index=sprPlayerStand;
        image_speed=0; //Don't animate
    }
    else //But if we are moving, change to walk sprite
    {
         //Reset image index if just now switching to walk sprite
        if (sprite_index!=sprPlayerWalk) image_index=0;
        sprite_index=sprPlayerWalk;
        image_speed=0.5; //Change this to whatever works for you
    }
}
else //If we're not grounded, change to in air sprite
{
sprite_index=sprPlayerInAir;
image_speed=0; //Don't animate
}


Thanks!
  • 0

#2 pure_evil020

pure_evil020

    GMC Member

  • New Member
  • 1479 posts

Posted 30 December 2010 - 03:15 AM

KEY_RIGHT=keyboard_check(vk_right);
KEY_LEFT=keyboard_check(vk_left);
KEY_JUMP=keyboard_check_pressed(vk_up);
RUN_KEY=keyboard_check(vk_shift);

if !RUN_KEY
{if KEY_RIGHT
{hspeed=2}}
if !RUN_KEY
{
if KEY_LEFT
{hspeed=-2}}

if RUN_KEY
{
if KEY_RIGHT
if hspeed<=10
{
hspeed+=1
}}
if RUN_KEY
{
if KEY_LEFT
if hspeed >=-10
{
hspeed-=1
}}

I made this up quickly in GM. put in the step event, it makes the object go at speed "2" if left or right is held, but if shift is held at the same time, it will raise the speed by 1 each step, giving you your gradual increase in speed.
I also put a check to make sure the speed doesn't go above 10, so there is a limit as to how fast you can get.
pm me if you have any questions
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users