This site requires JavaScript, please enable it in your browser!
Greenfoot back
trGhuul
trGhuul wrote ...

2021/3/28

Pressing a key once to make a Animation?

trGhuul trGhuul

2021/3/28

#
Hello. I have a side-scroller game and my hero has a melee attack animation. but my problem is that i have to hold the key (in my case "E") to go trough the animation-s.... but i want to press "E" once and the animation to go trough once. is there such a way, because i don't think of any. maybe use pngs instead of gifs but i rather would work with gifs.
if(Greenfoot.isKeyDown("E") && onGround())
        {
            if(DIRECTION =="LEFT")
            {
                setImage(RADL.getCurrentImage());
            }
            else
            {
                setImage(RADR.getCurrentImage());
            }
            moving = false;
            attacking = true;
        }
        else
        {
            attacking = false;
        }
FirewolfTheBrave FirewolfTheBrave

2021/3/29

#
I'd recommend you to work with states. Right at the beginning of your code where you declare your variables, declare an additional
private int state;
Then, change your code so that pressing E will cause the hero to enter a certain state which will keep executing the state action until something causes another state change. Maybe write something like this:
public void act()
{
     if (state == 1)
     {
          if (DIRECTION == "LEFT")
          {
               setImage (RADL.getCurrentImage());
          }
          else
          {
               setImage (RADR.getCurrentImage());
          }

          if (<the condition under which the state should exit>)
          {
               state = 0;
               attacking = false;
          }

     }

     else if (Greenfoot.isKeyDown("E") && onGround())  //By putting this into an else if() condition, you prevent your program from wasting computing capacity by checking for the conditions even if you're already in state 1.
     {
          state = 1;
          moving = false;
          attacking = true;  //I don't know what these variables do so I'm not touching them, you might find a better way of incorporating them into your states.
     }

    
}
You need to login to post a reply.