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:
Here's my code for obj_player_head
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;
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
}