• 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!

Character movement continues while attacking

NimNom1234

Member
Hello.
I have a feeling that the reason for error is something silly, as this is a pretty basic part of a games programming, but when my character attacks, he can still move freely, despite my efforts.
I was fed up with platformer programming, so I caved and used Shaun Spaldings code.

In the step event:
Code:
//Get Input
key_right = keyboard_check(ord("D"))
key_left = -keyboard_check(ord("A"))
key_jump = keyboard_check(ord("O"))


//React to Input
move = key_left + key_right
hsp = move * movespeed
if (vsp < 10) vsp += grav
if (place_meeting(x,y+1,obj_floor)) {
vsp = key_jump * -jumpspeed
}

if keyboard_check_pressed(ord("K")) {
    attacking = true
    image_speed = .3
    if direction = 1 {
    sprite_index = spr_char_attack_left
    }

    if direction = 2 {
    sprite_index = spr_char_attack_right
    }
}

if keyboard_check(ord("D")) && attacking = false {
direction = 2
image_speed = .3
sprite_index = spr_char_walk_right
}

if keyboard_check(ord("A"))  && attacking = false {
direction = 1
image_speed = .3
sprite_index  = spr_char_walk_left
}


//General Collision
if (place_meeting(x+hsp, y+vsp, obj_floor)) {
    hsp = 0;
}

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_floor)) {
    
    while !(place_meeting(x+sign(hsp),y,obj_floor)){
        x += sign(hsp)
    }
    hsp = 0
}

//Vertical Collision
if (place_meeting(x,y+vsp,obj_floor)) {
    
    while !(place_meeting(x,y+sign(vsp),obj_floor)){
        y += sign(vsp)
    }
    vsp = 0
}
x += hsp
y += vsp


//Check Idle
if !keyboard_check(ord("A")) && !keyboard_check(ord("D")) && attacking = false {
image_speed = 0
    if direction = 1 {
    sprite_index = spr_char_idle_left
    }
    if direction = 2 {
    sprite_index = spr_char_idle_right
    }
}

In the create event:
Code:
///Initialize Variables
direction = 2
attacking = false
grav = 1
hsp = 0
vsp = 0
jumpspeed = 7.5
movespeed = 3
in the Animation End event:
Code:
if sprite_index = spr_char_attack_left || spr_char_attack_right {
    attacking = false
    if direction = 1 {
    image_speed = .3
    sprite_index = spr_char_idle_left
    }

    if direction = 2 {
    image_speed = .3
    sprite_index = spr_char_idle_right
    }
}
 

FoxxiLoxi

Member
Your if statement has an error

if !keyboard_check(ord("A")) && !keyboard_check(ord("D")) && attacking = false

its should be attacking==false;

== for comparing
= for assignment

or you can use the If-ELSE feature
if attacking
else if left
else if right

so it can only do one
 

NimNom1234

Member
Your if statement has an error

if !keyboard_check(ord("A")) && !keyboard_check(ord("D")) && attacking = false

its should be attacking==false;

== for comparing
= for assignment

or you can use the If-ELSE feature
if attacking
else if left
else if right

so it can only do one
Do you mean like this?

if keyboard_check_pressed(ord("K")) {
attacking = true
image_speed = .3
if direction = 1 {
sprite_index = spr_char_attack_left
}
if direction = 2 {
sprite_index = spr_char_attack_right
}
}
else {
if keyboard_check(ord("D")) && attacking = false {
direction = 2
image_speed = .3
sprite_index = spr_char_walk_right
}
if keyboard_check(ord("A")) && attacking = false {
direction = 1
image_speed = .3
sprite_index = spr_char_walk_left
}
}




Didn't seem to work
 

FoxxiLoxi

Member
Code:
if keyboard_check_pressed(ord("K"))
{
       attacking = true
       image_speed = .3
       if direction ==1
       {
              sprite_index = spr_char_attack_left
       }
       if direction ==2
       {
             sprite_index = spr_char_attack_right
       }
}else if keyboard_check(ord("D")) && attacking == false
{
             direction = 2
             image_speed = .3
             sprite_index = spr_char_walk_right
}else if keyboard_check(ord("A"))  && attacking==false
{
             direction = 1
             image_speed = .3
             sprite_index = spr_char_walk_left
}
Something like this but if its this way you don't honestly need the attacking==false. Remember == : comparing =:assignment.
 
Last edited:

NimNom1234

Member
Code:
if keyboard_check_pressed(ord("K"))
{
       attacking = true
       image_speed = .3
       if direction ==1
       {
              sprite_index = spr_char_attack_left
       }
       if direction ==2
       {
             sprite_index = spr_char_attack_right
       }
}else if keyboard_check(ord("D")) && attacking == false
{
             direction = 2
             image_speed = .3
             sprite_index = spr_char_walk_right
}else if keyboard_check(ord("A"))  && attacking==false
{
             direction = 1
             image_speed = .3
             sprite_index = spr_char_walk_left
}
Something like this but if its this way you don't honestly need the attacking==false. Remember == : comparing =:assignment.
Thanks!
 

Relic

Member
The previous suggestions are not the issue. While some programming languages would require == for condition checking, GML does not. Your if statements are valid (in terms of syntax)

However, they don’t do what you intend based on your first post. Look at your code and work out how and why your character changes position.

Hints:
x+=hspd changes your x position.
Where is hspd determined? You need hspd to be zero when attacking.
 

FoxxiLoxi

Member
Tis true, but I think it's better to not teach a bad habit. Some people transition from Gamemaker to other languages. As for the other thing, I left it for him to figure out he had to move the movement to inside those conditions. Because even if he has it set to zero it will still trigger if the player was to hit the attack and the movement key. By how he has it coded

Code:
//React to Input
move = key_left + key_right<-this is what is setting the move so if it isn't zero hsp won't be zero.
hsp = move * movespeed
Because hsp gets it value from move, that gets its value from when the player hits one of the movement keys. There are other things in this code that could be cleaned up but I only replied to what he supplied me and stated 'something' like that. This was enough for him to see that the animation wouldn't change and he needed to make additional changes.
 
Last edited:

Relic

Member
I see. I did not notice the change in the if...else blocks you provided. Thought it was just showing the use of ==
 

Simon Gust

Member
Technically, you should check flag variables like this
Code:
if (flag) {}
and respectively
Code:
if (!flag) {}
You also wouldn't write keyboard / collision checks like this:
Code:
if (keyboard_check_pressed(ord("K")) == true) {}
// or
if (place_meeting(x, y, obj_floor) == true) {}
would you?
 
Top