Bobcraft420
Member
Hey, so I have been working on a top down RPG and I am trying to implement a state system to switch the players states i.e ( idle, movement, sprint,attack,etc). I'm fairly new to GMS(I'm using GMS 1.4.9999) and have some minor coding experience.
The problem I am stuck with is that when the player is in the idle state it does not switch to the attack state.
This is my idle state code
This is the movement state
Also when leaving the attack state to idle the sprite remains the attack sprite until I move the player.
Finally here is the attack state code
All that's in my create event for now with regards to the states is just setting the variable state to the idle state and my step event looks like this;
Thanks for any help in advance.
The problem I am stuck with is that when the player is in the idle state it does not switch to the attack state.
This is my idle state code
Code:
scr_get_input();
//if moving
if (up_key || down_key || right_key || left_key)
{
state = scr_move_state;
}
//if attacking
if(attack_key)
{
state = scr_attack_state;
}
//if sprite is facing down
if(sprite_index = spr_player)
{
image_speed = 0.1;
sprite_index = spr_male_idle
}
//if sprite is facing up
if(sprite_index = spr_male_up)
{
sprite_index = spr_male_idleu
image_speed = 0.1;
}
//if sprite is facing right
if(sprite_index = spr_male_right)
{
image_speed = 0.1;
sprite_index = spr_male_idler
}
//if sprite is facing left
if(sprite_index = spr_male_left)
{
image_speed = 0.1;
sprite_index = spr_male_idlel;
}
Code:
///scr_move_state
scr_get_input();
spd=1;
//if attacking
if(attack_key)
{
state = scr_attack_state;
}
// go to idle state
if (!down_key && !up_key && !left_key && !right_key) {
state = scr_idle_state;
}
//if sprinting
if(sprint_key)
{
state =scr_sprint_state;
}
//Move Down
if (down_key) {
phy_position_y += spd;
sprite_index = spr_player;
image_speed = .3;
}
//Move Up
if (up_key) {
phy_position_y -= spd;
sprite_index = spr_male_up;
image_speed = .3;
}
//Move Left
if (left_key) {
phy_position_x -= spd;
sprite_index = spr_male_left;
image_speed = .2;
}
//Move Right
if (right_key) {
phy_position_x += spd;
sprite_index = spr_male_right;
image_speed = .2;
}
Finally here is the attack state code
Code:
scr_get_input();
spd =0.05
//if not attacking
if(!attack_key)
{
state = scr_idle_state;
}
//if sprite is facing down
if(sprite_index = spr_player)
{
sprite_index = spr_male_attackd;
image_speed = .2;
}
//if sprite is facing up
if(sprite_index = spr_male_up)
{
sprite_index = spr_player_attacku;
image_speed = .2;
}
//if sprite is facing right
if(sprite_index = spr_male_right)
{
sprite_index = spr_male_attackr;
image_speed = .2;
}
//if sprite is facing left
if(sprite_index = spr_male_left)
{
sprite_index = spr_male_attackl;
image_speed = .2;
}
//Move Down
if (down_key)
{
phy_position_y += spd;
sprite_index = spr_male_attackd;
image_speed = .2;
}
//Move Up
if (up_key)
{
phy_position_y -= spd;
sprite_index = spr_player_attacku;
image_speed = .2;
}
//Move Left
if (left_key)
{
phy_position_x -= spd;
sprite_index = spr_male_attackl;
image_speed = .2;
}
//Move Right
if (right_key)
{
phy_position_x += spd;
sprite_index = spr_male_attackr;
image_speed = .2;
}
Code:
script_execute(state);