• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Trouble with direction and horizontal speed

J

Jmarlin3

Guest
I'm having trouble correctly programming the direction I want one of my objects to move. When Z is pressed, Obj_player_head is supposed to move the direction relative to what obj_player.image_xscale returns (this being 1 or -1). The problem is that while it does move to the right when Z is pressed, it will move 1 point to the left after Z is pressed, but once it's pressed again it attempts to move the right. If pressed again, it tries left, then right, and so on so forth. Here's my step event for obj_player:
Code:
/// Movements

//Get the player's inputs
//all of these return 1 or 0
key_right = keyboard_check(vk_right);//red means it's a constant
key_left = -keyboard_check(vk_left);//the '-' is there to return the negative of the
//original value
key_jump = keyboard_check_pressed(vk_space); //pressed means you have to pressed
//every single time

key_down = keyboard_check(vk_down);

//React to inputs
move = key_left + key_right; // based off numbers returned, that determines if move left or right
hsp = move * movespeed; //number returned from move times movespeed

if (vsp < 10) vsp += grav; //adds vsp to grav once it's less than 10 pixels

if (place_meeting(x,y+1,obj_wall)) //if one pixel below obj_player there is a wall
{   
    if (key_jump) vsp = -jumpspeed //sets to 0
}
    
//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while(!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x += sign(hsp);//sign returns 1 or -1 based
    //if hsp is positive or negative
    }
    hsp = 0; //stops moving
}

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
    while(!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y += sign(vsp);//sign returns 1 or -1 based
    //if vsp is positive or negative
    }
    vsp = 0; //stops moving
}
x += hsp; //add horizontal speed to x
y += vsp; //add vertical speed to y

//Animate
//this means if move is either 1 or -1, it flip the sprite vertically in that direction
if (move != 0) image_xscale = move;
Here's my code for obj_player_head
Code:
if (keyboard_check_pressed(ord("Z")))
{
    if (obj_player.image_xscale == -1){
        movespeed = obj_player.image_xscale * movespeed
        hsp = movespeed
    }
    else
    {
        if (obj_player.image_xscale == -1){
            movespeed = obj_player.image_xscale * movespeed
            hsp = movespeed
            
        }
        
    }
    
    x += hsp; //add horizontal speed to x
}
 

Cat

Member
I think the problem is with the else in obj_player_head. The code inside of that block is just a copy/paste of the above if which means that you're checking if a condition is NOT the case (else), then immediately checking if it IS the case (if image_xscale). As such, it never executes.
 
G

GmlProgrammer

Guest
You have written
image_xscale == -1 twice in your check.
Did you mean
image_xscale == 1
on the else statement?
 
Top