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

2015/10/14

How to change animations depending on the key pressed?

ando600 ando600

2015/10/14

#
Hello, I am trying to make a Mortal Kombat type game, and I don't know how to make the animation change depending on certain conditions. Here is my code: import greenfoot.*; /** * Write a description of class FighterTwo here. * * @author (your name) * @version (a version number or a date) */ public class FighterOne extends Actor { private String fighterState; private GifImage stand = new GifImage("fighterOneStand.gif"); private GifImage walkBack = new GifImage("fighterOneWalkBa.gif"); public void act() { setImage(stand.getCurrentImage()); while(Greenfoot.isKeyDown("a")) { setImage(walkBack.getCurrentImage()); } }
danpost danpost

2015/10/14

#
Using 'if' instead of 'while' would bring you a lot closer to what you need.
ando600 ando600

2015/10/14

#
It still pauses whether i use if or while. I need the animations to run separately (different threads or something like that), because if i use and if or while statement, it gets caught in a single act cycle, and cannot retrieve the next frame in the animation sequence.
danpost danpost

2015/10/14

#
Try the following act method:
public void act()
{
    GifImage currentGif = stand;
    if (Greenfoot.isKeyDown("a")) currentGif = walkBack;
    setImage(currentGif.getCurrentImage();
}
From what I can tell, it probably would not make any difference from the above code using 'if' instead of 'while'; but, it does break down the code to the basics while only using 'setImage' once within the act method. I really cannot see how the running of the scenario could be caught in a single act cycle when using 'if', however. Maybe there is an issue somewhere else in your code.
ando600 ando600

2015/10/14

#
This is my code: import greenfoot.*; /** * Write a description of class FighterTwo here. * * @author (your name) * @version (a version number or a date) */ public class FighterOne extends Actor { private int speed = 4; private int vSpeed = 0; private int acceleration = 1; private int jumpStrength = 18; private GifImage gif = new GifImage("fighterOneStand.gif"); /** * Check keyboard input and act accordingly */ public void act() { setImage(gif.getCurrentImage()); checkKeys(); checkFall(); } private void checkKeys() { if (Greenfoot.isKeyDown("right")) { setImage("walk.gif"); move(4); } if (Greenfoot.isKeyDown("left")) { // setImage("walkback.gif"); move(-4); } if (Greenfoot.isKeyDown("up")) { // setImage("jump.gif"); jump(); // Greenfoot.playSound("jump.wav"); } } public void jump() { if (onRoad()) { vSpeed = -jumpStrength; fall(); } } public void checkFall() { if(onRoad()) { vSpeed = 0; } else { fall(); } } public boolean onRoad() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight() / 2-8, Road.class); return under !=null; } public void fall() { setLocation ( getX(), getY() + vSpeed ); vSpeed = vSpeed + acceleration; } public void moveRight() { setLocation ( getX() - speed, getY() ); } public void moveLeft() { setLocation ( getX() - speed, getY() ); } }
ando600 ando600

2015/10/14

#
And if i change the setImage method to setImage(gif.getCurrentImage()); it makes no difference.
ando600 ando600

2015/10/14

#
I know that i dont have all of the gif image variables set up, but i just need one to work first.
ando600 ando600

2015/10/14

#
Ok. I Figured it out. Thank you! I Just had to take my set image commands out of private methods, as they could not modify the image. Thank you!
danpost danpost

2015/10/14

#
Maybe you should have a field to hold the current GifImage object you are using. Then, depending on the keys set the field value and at the end get the image of whichever gif object is set as current.
danpost danpost

2015/10/14

#
ando600 wrote...
Ok. I Figured it out. Thank you! I Just had to take my set image commands out of private methods, as they could not modify the image. Thank you!
I did not see that as an issue. What I did see was you trying to use the GifImage object itself as a parameter in the 'setImage' method (which requires a GreenfootImage object). So. it will not work using "walk.gif" or "walkBack.gif"; but, it will when you use 'getCurrentImage' on a GifImage object (which returns a GreenfootImage object).
ando600 ando600

2015/10/14

#
No. Your code was right. I implemented your code, but also had to change the location of some variables, as they could not be modified outside of private methods. Once i did that, you code worked great. Thanks again!
You need to login to post a reply.