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

2014/10/1

Assistance needed with an animation bug

nickthequik nickthequik

2014/10/1

#
So I am writing code to animate the player's actor in a side scroller platformer and I have run into a problem. The animate() method I show below works fine as I'm moving to the right; however once I start to go back to the left, the animations cycle between one set of right facing pictures and one set of left facing pictures. Also, when I start to go back to the right after going left, the animations stay messed up and continue to cycle one right facing and one left facing. All the .pngs are drawn with the character facing to the right, so I figured I could simply mirror them instead of creating another set that face left. Can anyone tell from my code where the error is? I have 13 images that compose the animation, but does anyone have any suggestions for how to control the speed of the function so that I do not need to insert copies of the same picture to make the animation seem slower. Any help is appreciated!
public void animate(){
        if (Greenfoot.isKeyDown("right")){
            currentImage = (currentImage + 1) % images.length;
            setImage(images[currentImage]);
        }
        else if (Greenfoot.isKeyDown("left")){
            currentImage = (currentImage + 1) % images.length;
            GreenfootImage[] reverse = images;
            reverse[currentImage].mirrorHorizontally();
            setImage(reverse[currentImage]);
        }
        else if (Greenfoot.isKeyDown("left") != true && Greenfoot.isKeyDown("right") != true){
            avatar = new GreenfootImage("player0.png");
            setImage(avatar);
        }
    }
images is a GreenfootImage array and reverse is essentially a copy of images that I created to test if the images in images are being permanently mirrored. Also, this animate() method is in the act() method for the player. It is called after another method that is used to decide if the player should move or not which is used in the scrolling effect.
Super_Hippo Super_Hippo

2014/10/1

#
With line 8, you make 'reverse' and 'images' the same. If you change one, you also change the other. So instead, you need to create a new GreenfootImage from 'image'. It's the first constructor of the class 'GreenfootImage':
API wrote...
GreenfootImage(GreenfootImage image) Create a GreenfootImage from another GreenfootImage.
Btw, you can change line 12 to 'else' and remove the rest. And with the last problem, you can have a timer counting up when a image is active and when it reaches a certain number, you change the image and set the timer back to 0.
danpost danpost

2014/10/1

#
What Super_Hippo means by you making the arrays the same is that when you create the reference 'reverse', you are telling the compiler to have it point to the same memory location that 'images' points. That is why when you mirror an element in one array, you are actually mirroring the element in the other as well, because they are not two distinct images, but one in the same. Rather than copy, or create, the entire array, it would be better just to get the one image needed at that time and mirror it, replacing lines 8 through 10 with this:
GreenfootImage reversed = new GreenfootImage(images[currentImage]);
reversed.mirrorHorizontally();
setImage(reversed);
This is, however, more work than what needs to be done for one act cycle and excessive work with images can produce lag. I would suggest having the 'reverse' array created and stored in an instance or static array (just like the 'images' array is) to reduce the workload during the simulation.
You need to login to post a reply.