What am I doing wrong with this code?

Started by
6 comments, last by alvaro 10 years, 1 month ago

Ok. So, i'm making a basic 2d top-down rpg. Everything is working, it's just one thing that's kind of funny. My attack animation works...but the animation is triggered whenever I move in any direction. Can anyone help me out? I know this code could probably be structured better, but I'm just starting so bare with me.

{
if(keyboard_check(vk_right) && place_free(x+4,y)&& sprite_index != spr_playerattack) {
x+=4;
sprite_index = spr_playerrun;
image_speed = .2;
image_xscale = 1;
}
if(keyboard_check(vk_left) && place_free(x-4,y) && sprite_index != spr_playerattack){
x-=4;
sprite_index = spr_playerrun;
image_speed = .2;
image_xscale = -1;
}
if(keyboard_check(vk_up) && place_free(x,y-4) && sprite_index != spr_playerattack){
y-=4;
sprite_index = spr_playerrun;
image_speed = .2;
image_xscale = 1;}
if(keyboard_check(vk_down) && place_free(x,y+4) && sprite_index != spr_playerattack){
y+=4;
sprite_index = spr_playerrun;
image_speed = .2;
image_xscale = 1;
}
if(keyboard_check_pressed(ord('C') && sprite_index != spr_playerattack)) {
sprite_index = spr_playerattack;
image_speed = .25;
}
if(!keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_down) && !keyboard_check(vk_up) && sprite_index != spr_playerattack) {
image_speed = 0;
sprite_index = spr_playerstand;
}
if(global.hp <0)game_end();
}
Any help would be greatly appreciated!
Advertisement


if(keyboard_check_pressed(ord('C') && sprite_index != spr_playerattack)) {

sprite_index = spr_playerattack;

image_speed = .25;

}

I think you want to check if "C" is pressed and then if the current "sprite_index" is not "spr_playerattack" then "attack".... Right?

If so, your condition is unpredictable. Perhaps it should be:


if(keyboard_check_pressed(ord('C')) && (sprite_index != spr_playerattack)) {

sprite_index = spr_playerattack;

image_speed = .25;

}

Notice the difference? I separated the two conditions, is "C" pressed and "is it not attacking currently".

Ahhhh, I do see that!

Thank you for the quick reply. I'm going to go try it out right now!

It worked! It's amazing how one little thing can make that big of a difference. Thanks, again. It is much appreciated!


It's amazing how one little thing can make that big of a difference.

Happens to me all the time.

Instead of testing “sprite_index != spr_playerattack” a million times you could just test it once and put the rest of the code inside that if-statement…


if ( sprite_index  != spr_playerattack ) {
    if(keyboard_check(vk_right) && place_free(x+4,y)) {
        x+=4;
        sprite_index = spr_playerrun;
        image_speed = 0.2f; // Don’t use constant doubles in place of floats.  .2 should be 0.2f.
        image_xscale = 1;
    }
    if(keyboard_check(vk_left) && place_free(x-4,y)){
        x-=4;
        sprite_index = spr_playerrun;
        image_speed = 0.2f; // Don’t use constant doubles in place of floats.  .2 should be 0.2f.
        image_xscale = -1;
    }
    // etc.
}
It would really help your readability and maintenance.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Instead of testing “sprite_index != spr_playerattack” a million times you could just test it once and put the rest of the code inside that if-statement…


if ( sprite_index  != spr_playerattack ) {
    if(keyboard_check(vk_right) && place_free(x+4,y)) {
        x+=4;
        sprite_index = spr_playerrun;
        image_speed = 0.2f; // Don’t use constant doubles in place of floats.  .2 should be 0.2f.
        image_xscale = 1;
    }
    if(keyboard_check(vk_left) && place_free(x-4,y)){
        x-=4;
        sprite_index = spr_playerrun;
        image_speed = 0.2f; // Don’t use constant doubles in place of floats.  .2 should be 0.2f.
        image_xscale = -1;
    }
    // etc.
}
It would really help your readability and maintenance.


L. Spiro

Thanks for the help! :D Much appreciated!

I prefer this type of code (not tested):
  struct {                                                                                                            
    int key;                                                                                                          
    int delta_x;
    int delta_y;                                                                                             
    int xscale;                                                                                                       
  } const directions[4] = {                                                                                           
    {vk_right, 4, 0, 1},                                                                                              
    {vk_left, -4, 0, -1},                                                                                             
    {vk_up, 0, -4, 1},                                                                                                
    {vk_down, 0, 4, 1}                                                                                                
  };                                                                                                                  
                                                                                                                      
  if (sprite_index != spr_playerattack) {                                                                             
    for (auto direction : directions) {                                                                               
      if (keyboard_check(direction.key) && place_free(x + direction.delta_x, y + direction.delta_y)) {                
        x += direction.delta_x;                                                                                       
        y += direction.delta_y;                                                                                       
        sprite_index = spr_playerrun;                                                                                 
        image_speed = .2;                                                                                             
        image_xscale = direction.xscale;                                                                              
      }                                                                                                               
    }                                                                                                                 
  }                                                                                                                   

This topic is closed to new replies.

Advertisement