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

2012/12/26

Help with quick changing images

seanyou100 seanyou100

2012/12/26

#
Pretty simply I'm making a greenfoot program for my hsc. Now my program is closely based of the "little-crab" scenario but with a difference. my major character, a computer, will shut and close as it moves so it looks better and so my code becomes a bit more complex. the problem is at the speed i want the program to run (about halfway) the two images switch rapidly. Can i have some help to slow them down please? Here's the code so far; public class laptop extends Actor { private GreenfootImage laptop1 = new GreenfootImage("Laptop5.png"); private GreenfootImage laptop2 = new GreenfootImage("LaptopClosed2.png"); private boolean image1 = true; code code code.... private void walk() { if (getImage() == laptop1) { setImage(laptop2); } else { setImage(laptop1); } move(2); }
vonmeth vonmeth

2012/12/27

#
A simple counter would do fine. Increase the counter by one each act cycle. When the counter is under a number, set the image to one, when it is over that number set to two, and when it reaches another number, set it back down to zero to start the cycle over again.
seanyou100 seanyou100

2012/12/27

#
Thank for the help Vonmeth but please understand I'm very new to this. I'm not gonna lie have no idea what you said, but i would be very thankful if you could demonstrate a more practical situation of what you said. thanks.
vonmeth vonmeth

2012/12/27

#
Bit different from what I described above (just did 'equal to' instead of 'greater/less than' so as not constantly be setting the image each cycle). Adjust numbers as you see fit.
    private int animationCounter;
	
    private void walk()
    {
        if (animationCounter == 80) animationCounter = 0; // needs to be above the line below, do you see why?
        if (animationCounter == 0)  setImage(laptop1);        // see what happens when this is above the line above if you don't understand why
        if (animationCounter == 40) setImage(laptop2);
        
        animationCounter++; // increments animationCounter by 1
        move(2);
    }
seanyou100 seanyou100

2012/12/27

#
Thanks. It works perfectly now :D
You need to login to post a reply.