Advertisement Jump to content
Sign in to follow this  
Jonathan Ryan

What am I doing wrong with this code?

This topic is 1795 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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 Edited by L. Spiro

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
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;                                                                              
      }                                                                                                               
    }                                                                                                                 
  }                                                                                                                   

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!