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

2014/7/5

wait and walking on platforms

Moho Moho

2014/7/5

#
hello. when i try to compile my code it says "unreported java......." and it points out the wait code. i have a method for animation and its like this
public void animate()
{
setImage("walk1");
wait(6); //idk it just doesnt let me compile while the wait is there
setImage("walk2");

}
the other thing is walking on platforms. im not sure how to do that and if the platform should be a world or an object within the world. can you just write me the code for walking on a platform.thanks in advance.
danpost danpost

2014/7/5

#
First off, you should not use 'wait' at all, unless you know what you are doing. Both 'wait' and its alternative, 'delay', will not perform as you might think. They will freeze all your actors until the animate method is completed (that includes the 'wait' or 'delay' time. Also, as you have it coded above the "walk2" image will show for one act cycle while the "walk1" image will show for 6 system execution ticks (or something like that). To animate properly, you need to add an instance int field to be a timer for the images (how long each one is being used for). Something like this:
private int imageTimer;
private int maxImageTime = 15;

public void animte()
{
    if (imageTimer%maxImageTime == 0)
    {
        if (imageTimer == 0) setImage("walk1" /* +".png" */); // or whatever
        else setImage("walk2" /* +".png" */); // or whatever
    }
    imageTimer = (imageTimer+1)%(maxImageTime*2);
}
You can adjust the value of 'maxImageTime' to suit your needs (I added that field to make it more easily adjustable for you; so you do not have to search through the code and change the hard-coded values individually). The last line will increment the timer until it reaches twice the value of 'maxImageTime; so it runs for both images and then resets to zero. The 'if' condition on line 6 will only be true when the value of 'imageTimer' is zero or at the value of 'maxImageTimer'. I cannot tell whether you added a 'setImage(String)' overriding method to your class; but, if not, you need to concatenate (or include) the filename suffixes to your image String names.
Moho Moho

2014/7/7

#
@danpost it worked perfectly thanks! but how do i set the image back to the main image ( standing up straight ). the image is saved as Stand.fw.png
danpost danpost

2014/7/7

#
That code would need to be placed somewhere around that which you call 'animate' from:
if (/* some condition */) animate(); else setImage("Stand.fw.png");
Something like that.
You need to login to post a reply.