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

2012/10/14

Need help changing actor image

1
2
Builderboy2005 Builderboy2005

2012/10/17

#
That is a less than ideal solution though. Not only are you creating a completely new image each time you check, but you also have to compare the images pixel by pixel to see if they are equal.
limefortheworld limefortheworld

2012/10/17

#
I suggest making a static reference greenfootimage, then make copies off the static reference rather than making a completely new image. Also, might I suggest a "frame" variable, and if so, also suggest using the ArrayList to help with the organization. If using the frame system (assuming the frame changes every act),
import java.util.ArrayList;

static ArrayList<GreenfootImage> images = new ArrayList<GreenfootImage>();
private int frame;

static public void initialize()
{
images.add(new GreenfootImage("image 1 name"));
images.add(new GreenfootImage("image 2 name"));
...
}

public void act()
{
frame ++;
if(frame >= images.size())
{
frame = 0;
}
setImage(images.get(frame));
}
Ensure to run the "initialize" variable once. (I usually do this at the World constructor)
(Classname).initialize();
This is okay assuming you are not manipulating the images. If not, then substitute setImage(images.get(frame)); with setImage(new GreenfootImage(images.get(frame)));
Builderboy2005 Builderboy2005

2012/10/17

#
That is a lot of work that could be sidestepped simply by keeping track of your current image by using a boolean or int.
limefortheworld limefortheworld

2012/10/18

#
Builderboy2005 wrote...
That is a lot of work that could be sidestepped simply by keeping track of your current image by using a boolean or int.
frame = keeping track of current image I specifically chose the static ArrayList method with the assumption that he will be fielding multiple objects of the class.
Builderboy2005 Builderboy2005

2012/10/18

#
Oh I see, I misunderstood you. For some reason I thought you were proposing a solution that involved saving global static images for the purpose of *comparing* them XD My mistake!
limefortheworld limefortheworld

2012/10/18

#
lulzy lulz Just about all of my games involve this frame mechanics (although it is only with my later works that I implement the static global variables) involving multiple images for actors (alongside the additional framechange variable). But then my stuff isn't as nearly as great as yours...
You need to login to post a reply.
1
2