TIGSource Forums

Developer => Technical => Topic started by: Rat Casket on January 09, 2013, 03:49:12 PM



Title: Changing Movement Speed
Post by: Rat Casket on January 09, 2013, 03:49:12 PM
I'm having a simple problem here that I just cant seem to figure out. The answer has to be staring me in the face but I'm new to all of this so I'd like some help.

What I'd like to achieve is slower movement while crouched. I have the character able to crouch, and slow down while doing so, but he does not revert to his normal walking speed when standing back up. He stays moving at the decreased crouch speed.

What little thing am I missing here?

Create
Code:
//Movement

defSpd = 5;
walkSpd = 5;
runSpd = 10;
blkSpd = 2;

Step
Code:
//crouch
if (keyboard_check(vk_down))
    {
    sprite_index = sPlayerDuck
    defSpd = blkSpd;
    }
else
    {
    if place_free(x,y-1)
        sprite_index = sPlayerStand
    }

And yes, I am aware I'm missing any and all code to change the movement speed back to normal, but I can't figure out how to do it so I've left it out of this example.


Title: Re: Changing Movement Speed
Post by: feminazi on January 09, 2013, 03:59:45 PM
come on, try something before you ask. if you know what "else" means it should be simple

Code:
if (keyboard_check(vk_down))
{
    //crouching
    spd = crouchspd;
}
else
{
    //not crouching
    spd = walkspd;
}

you're probably confused because you have another if statement inside of the else block. dont fret aobut that

Code:
if (keyboard_check(vk_down))
{
    //crouching
    sprite_index = sPlayerDuck;
    defSpd = blkSpd;
}
else
{
    //not crouching
    defSpd = walkSpd;
    if (place_free(x,y-1))
    {
        //not crouching and is standing
        sprite_index = sPlayerStand;
    }
}


Title: Re: Changing Movement Speed
Post by: Rat Casket on January 09, 2013, 04:12:23 PM
Well, I did try it. I've been trying it for a while and I couldn't get it to work. This is some of the first code I've ever written in my entire existence so cut me some slack.

What you posted did work though. I realize now where I was making my mistake.

Edit:

After cleaning some stuff up, I have run into a new problem. Again I'm sure its something simple but I'm really new to this so please forgive my ignorance.

I have 3 speeds in place. Walk, crouch, and run. With the current config I am able to move and run at their respective speeds. I am also able to crouch and I am unable to sprint while crouched, which is what I want. However when I crouch my speed doesnt reflect that. I stay moving at the same speed as if I were walking.

Code:
//Movement
//left and right
if (keyboard_check(vk_right)) && place_free(x+1, y){
    x += defaultSpd;
}
   if (keyboard_check(vk_left)) && place_free(x-1, y){
    x -= defaultSpd;
}
//crouch
if (keyboard_check(vk_down)){
    crouch = true;
}else{
    crouch = false;
}
    if (crouch){
    defaultSpd = crouchSpd;
    sprite_index = sPlayerDuck
}
    if (!crouch && place_free(x,y-1)){
    defaultSpd = walkSpd;
    sprite_index = sPlayerStand
}
//sprint
if (keyboard_check(vk_shift)) && (sprite_index = sPlayerStand){
    running = true;
}else{
    running = false;
}
    if (running){
    defaultSpd = runSpd;
}
    if (!running){
    defaultSpd = walkSpd;
}

And here is the gm file if you'd like it to test with.
http://dl.dropbox.com/u/28435104/mendoza.gm81


Title: Re: Changing Movement Speed
Post by: feminazi on January 09, 2013, 09:06:13 PM
if (!running){
    defaultSpd = walkSpd;
}

this last parts causing it

you want to set it back to walkspeed it its not running but you don't want to set it back to walkspeed if you're crouching (you want to keep it crouchspeed)

if (!running && !crouching){
    defaultSpd = walkSpd;
}

code order matters
if i did
speed = 2
speed = 3
speed will always be 3


Title: Re: Changing Movement Speed
Post by: Rat Casket on January 09, 2013, 09:25:01 PM
That fixed it! There are a few collision bugs here and there but I can mess with those later. Thanks a ton man.

My order is sloppy to say the least. Any advice? Here is my current code since making your suggested corrections, and adding jumping/gravity.

Code:
//jump
if (place_empty(x,y+1)) {
    falling = true;
        gravity=0.9;
        gravity_direction = 270;
}else{
    falling = false;
        gravity=0;
}
if(keyboard_check_pressed(vk_space)) && (!crouch){
    if(!place_free(x,y+1))
    vspeed = -10;
}
//left and right
if (keyboard_check(vk_right)) && place_free(x+1, y){
    x += defaultSpd;
}
   if (keyboard_check(vk_left)) && place_free(x-1, y){
    x -= defaultSpd;
}
//crouch
if (keyboard_check(vk_down)) && (!falling){
    crouch = true;
}else{
    crouch = false;
}
    if (crouch){
    defaultSpd = crouchSpd;
    sprite_index = sPlayerDuck
}
    if (!crouch && place_free(x,y-1)){
    defaultSpd = walkSpd;
    sprite_index = sPlayerStand
}
//sprint
if (keyboard_check(vk_shift)) && (sprite_index = sPlayerStand){
    running = true;
}else{
    running = false;
}
    if (running){
    defaultSpd = runSpd;
}
    if (!running && !crouch) && (sprite_index = sPlayerStand){
    defaultSpd = walkSpd;
}