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

2012/3/18

How to avoid image flickering

nachocab nachocab

2012/3/18

#
I am programming a new project and I have the problem of flickering images, Does somebody know how to avoid this problem or how to make a bufered image in Greenfoot?
Builderboy2005 Builderboy2005

2012/3/18

#
Greenfoot automatically double buffers it's display, would you mind posting your code? There might be something you are doing that is causing unnecessary flicker.
nachocab nachocab

2012/3/19

#
This is my code, I think that the image is not so small and this is the problem, thank you for your reply to my question. I will try with small images import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Tigre here. * * @author (your name) * @version (a version number or a date) */ public class Tigre extends Actor { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; public Tigre(){ image1 = new GreenfootImage("tigre01.png"); image2 = new GreenfootImage("tigre02.png"); image2 = new GreenfootImage("tigre03.png"); setImage(image1); } /** * Act - do whatever the Tigre wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(getImage()==image3){ setImage(image1); } else if(getImage()==image1){ setImage(image2); } else if(getImage()==image2){ setImage(image3); } move(10); } }
danpost danpost

2012/3/19

#
I believe the problem lies in the fact that there is no time interval between the changing of the images. So, either the speed slider has to be turned down fairly low, or you need to add a timer to regulate the speed of the changing of the images.
Duta Duta

2012/3/19

#
nachocab wrote...
This is my code, I think that the image is not so small and this is the problem, thank you for your reply to my question. I will try with small images import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Tigre here. * * @author (your name) * @version (a version number or a date) */ public class Tigre extends Actor { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; public Tigre(){ image1 = new GreenfootImage("tigre01.png"); image2 = new GreenfootImage("tigre02.png"); image2 = new GreenfootImage("tigre03.png"); setImage(image1); } /** * Act - do whatever the Tigre wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(getImage()==image3){ setImage(image1); } else if(getImage()==image1){ setImage(image2); } else if(getImage()==image2){ setImage(image3); } move(10); } }
Firstly, what danpost said is the cause of the flickering problem. There are also a couple of other problems with your code: - In your if(...) checks you are doing them along the lines of if(getImage()==<anImage>), however as you are comparing 2 objects (2 GreenfootImage objects in this case) you should use if(<anObject>.equals(<anotherObject>)), which in this case would be written as if(getImage().equals(<anImage>)). - When you are setting your image<x> variables in your constructor, you are setting image2 twice (you set image1, then image2, and then instead of image3 you've set image2 again). While this is just a typo, it has an impact! - Finally here's a more concise way of doing your code:
private GreenfootImage[] images = new GreenfootImage[3];
private int pos = 0;

public Tigre(){
    for(int i = 0; i < images.length; i++)
        images[i] = new GreenfootImage("tigre0" + ((int)(i+1)) + ".png");
    setImage(images[0]);
}

public void act()
{
    pos++;
    if(pos >= images.length) pos = 0;
    setImage(images[pos]);
}
davmac davmac

2012/3/19

#
- In your if(...) checks you are doing them along the lines of if(getImage()==<anImage>), however as you are comparing 2 objects (2 GreenfootImage objects in this case) you should use if(<anObject>.equals(<anotherObject>)), which in this case would be written as if(getImage().equals(<anImage>)).
This isn't correct, in this case. The three object references are created when the class is constructed; it's perfectly valid to use '==' to compare object references. If you 'setImage(image1)', then 'getImage() == image1' will be true. (There are definitely cases where you should use .equals() instead of '==', though; String comparison especially comes to mind).
Duta Duta

2012/3/19

#
@davmac Ah, ok. I thought it might be valid due to the fact that he clearly hadn't ran into any errors regarding that, however I decided to include it anyway as I generally go with the rule of when comparing objects, use .equals(...) unless null checking. And yeah, String comparison was where I first learnt about .equals(...) (as I'm sure is the case for most people). Thanks for the tip :)
nachocab nachocab

2012/3/19

#
Thnaks a lot for your replies, I was thinking in an array of images like davmac said. I'm not sure if there is a difference in usgin == or equals but the use of equals sounds more for usefull for Strings. Thanks a lot for the tips Duta and davmac!!!
You need to login to post a reply.