Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


What am I doing wrong with this code?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Goozen   Members   -  Reputation: 102

Like
0Likes
Like

Posted 18 February 2014 - 03:00 PM

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!


Sponsor:

#2 Hawkblood   Members   -  Reputation: 726

Like
4Likes
Like

Posted 18 February 2014 - 03:21 PM


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".



#3 Goozen   Members   -  Reputation: 102

Like
0Likes
Like

Posted 18 February 2014 - 03:27 PM

Ahhhh, I do see that!

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



#4 Goozen   Members   -  Reputation: 102

Like
0Likes
Like

Posted 18 February 2014 - 03:29 PM

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



#5 Hawkblood   Members   -  Reputation: 726

Like
0Likes
Like

Posted 18 February 2014 - 03:33 PM


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

Happens to me all the time.



#6 L. Spiro   Crossbones+   -  Reputation: 18131

Like
4Likes
Like

Posted 18 February 2014 - 05:33 PM

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, 18 February 2014 - 05:34 PM.


#7 Goozen   Members   -  Reputation: 102

Like
0Likes
Like

Posted 19 February 2014 - 12:40 PM

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!



#8 Álvaro   Crossbones+   -  Reputation: 15390

Like
1Likes
Like

Posted 19 February 2014 - 08:05 PM

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;                                                                              
      }                                                                                                               
    }                                                                                                                 
  }                                                                                                                   





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS