B
bhoyle753
Guest
What I'm trying to make happen is, whenever the right_key is pressed show the sprite spr_player_right but however still show the sprite spr_player_right whenever moving diagonally up or downward. Same for moving left, up, and down.
The challenge with this is that when moving upward and suddenly you press the right_key or left_key to move diagonally whilst moving up I want the sprite to still show as spr_player_up and not for it to change to spr_player_left or spr_player_right.
(I want the same to happen with both the left and down movements of course)
I'm thinking that I need to somehow tell gamemaker to specifically check which button was pressed first. For example, If the right_key was pressed first and then the down_key or up_key was pressed after set the sprite to stay as spr_player_right. But if the down_key was pressed first and then the right_key was pressed after keep the sprite to be spr_player_down.
You can see which type of movement I am trying to achieve by watching some Zelda: a link to the past gameplay and watch how link can move diagonally whilst staying facing right and whilst facing down etc
Here is most of my code If you need it:
The challenge with this is that when moving upward and suddenly you press the right_key or left_key to move diagonally whilst moving up I want the sprite to still show as spr_player_up and not for it to change to spr_player_left or spr_player_right.
(I want the same to happen with both the left and down movements of course)
I'm thinking that I need to somehow tell gamemaker to specifically check which button was pressed first. For example, If the right_key was pressed first and then the down_key or up_key was pressed after set the sprite to stay as spr_player_right. But if the down_key was pressed first and then the right_key was pressed after keep the sprite to be spr_player_down.
You can see which type of movement I am trying to achieve by watching some Zelda: a link to the past gameplay and watch how link can move diagonally whilst staying facing right and whilst facing down etc
Here is most of my code If you need it:
Code:
/// scr_move_state
right_key = keyboard_check(vk_right) or (keyboard_check(ord("D")));
left_key = keyboard_check(vk_left) or (keyboard_check(ord("A")));
up_key = keyboard_check(vk_up) or (keyboard_check(ord("W")));
down_key = keyboard_check(vk_down) or (keyboard_check(ord("S")));
// Get the axis
var xaxis = (right_key - left_key);
var yaxis = (down_key - up_key);
// Check for gamepad input
if (gamepad_is_connected(0)) {
gamepad_set_axis_deadzone(0, .35);
xaxis = gamepad_axis_value(0, gp_axislh);
yaxis = gamepad_axis_value(0, gp_axislv);
}
// Get direction
var dir = point_direction(0, 0, xaxis, yaxis);
// Get the length
if (xaxis == 0 and yaxis == 0) {
len = 0;
} else {
len = spd;
}
// Get the hspd and vspd
hspd = round(lengthdir_x(len, dir));
vspd = round(lengthdir_y(len, dir));
// Move
phy_position_x += hspd;
phy_position_y += vspd;
if down_key = true {
sprite_index = spr_player_down
}
if up_key = true {
sprite_index = spr_player_up
}
if right_key = true {
sprite_index = spr_player_right
}
if left_key = true {
sprite_index = spr_player_left
}
// Correct direction sprites
if right_key = true and down_key = true {
script_execute(scr_move_state_right)
} else if right_key = true and up_key = true {
script_execute(scr_move_state_right())
}
if left_key = true and down_key = true {
script_execute(scr_move_state_left)
} else if left_key = true and up_key = true {
script_execute(scr_move_state_left())
}
if down_key = true and right_key = true {
script_execute(scr_move_state_down)
} else if down_key = true and left_key = true {
script_execute(scr_move_state_down())
}
if up_key = true and right_key = true {
script_execute(scr_move_state_up)
} else if up_key = true and left_key = true {
script_execute(scr_move_state_up())
}
// Changing sprite to correct idle sprite
if sprite_index = spr_player_right and (hspd = 0) {
sprite_index = spr_player_right_idle
}
if sprite_index = spr_player_left and (hspd = 0) {
sprite_index = spr_player_left_idle
}
if sprite_index = spr_player_down and (vspd = 0) {
sprite_index = spr_player_down_idle
}
if sprite_index = spr_player_up and (vspd = 0) {
sprite_index = spr_player_up_idle
}
/// scr_move_state_up
sprite_index = spr_player_up
/// scr_move_state_down
sprite_index = spr_player_down
/// scr_move_state_right
sprite_index = spr_player_right
/// scr_move_state_left
sprite_index = spr_player_left
Here are my obj_player properties:
Step Event:
event_inherited();
script_execute(state);
Create Event:
event_inherited();
spd = 1;
state = scr_move_state;
hspd = 0;
vspd = 0;
len = 0;
xaxis= 0;
yaxis = 0;
dir = 0;
Draw Event:
draw_sprite(spr_player_shadow, image_index, x, y);
draw_self();
I also have created a parent object so I can have the same collisions code for both the enemies and the player itself (mainly to keep things organized better) Here are the properties for it:
Create Event:
phy_fixed_rotation = true;
Step Event:
/// Control depth
depth = -y