Character movement continues while attacking

Discussion in 'Programming' started by NimNom1234, Aug 28, 2018.

  1. NimNom1234

    NimNom1234 Member

    Joined:
    Mar 31, 2018
    Posts:
    62
    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
        }
    }
    
     
  2. FoxxiLoxi

    FoxxiLoxi Member

    Joined:
    Feb 1, 2017
    Posts:
    29
    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 likes this.
  3. NimNom1234

    NimNom1234 Member

    Joined:
    Mar 31, 2018
    Posts:
    62
    Aight
     
  4. NimNom1234

    NimNom1234 Member

    Joined:
    Mar 31, 2018
    Posts:
    62
    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
     
  5. FoxxiLoxi

    FoxxiLoxi Member

    Joined:
    Feb 1, 2017
    Posts:
    29
    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: Aug 28, 2018
    NimNom1234 likes this.
  6. NimNom1234

    NimNom1234 Member

    Joined:
    Mar 31, 2018
    Posts:
    62
    Thanks!
     
  7. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    719
    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.
     
  8. FoxxiLoxi

    FoxxiLoxi Member

    Joined:
    Feb 1, 2017
    Posts:
    29
    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: Aug 29, 2018
  9. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    719
    I see. I did not notice the change in the if...else blocks you provided. Thought it was just showing the use of ==
     
  10. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,174
    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?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice