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
HayleyG HayleyG

2012/10/14

#
if (getImage() = "square1.png") { setImage("square2.png"); }
MatheMagician MatheMagician

2012/10/14

#
This should work:
if (getImage() == new GreenfootImage("square1.png"))
{
setImage(new GreenfootImage("square2.png"));
}
Your main problem was that you were telling the computer to set the image to a string. What you needed waas something that took that string, figured out what image it was refering to, and then create a copy of that image that is usable by setImage().
HayleyG HayleyG

2012/10/14

#
That compiled but nothing happened when the game was running.
MatheMagician MatheMagician

2012/10/14

#
I am not sure if the getImage() == new GreenfootImage("square1.png" is the best way to do it, but I do not know how I would improve it.
HayleyG HayleyG

2012/10/14

#
thanks for trying
MatheMagician MatheMagician

2012/10/14

#
You could try to use a different method of deciding whether or not to set the image to that.
danpost danpost

2012/10/15

#
Try: if (getImage().equals(new GreenfootImage("square1.png")) If that does not work, then you will probably need either a boolean instance variable or an int instance variable to keep track of which image is currently being used (as an alternative, an instance String variable to hold the current image file name).
SPower SPower

2012/10/15

#
For MathrMagician, you're comparing the pointers of the 2 GreenfootImages, but they are different since they ar e 2 different objects. The equals method compares the objects themselves, so danposts solution should work.
Builderboy2005 Builderboy2005

2012/10/15

#
You should create a separate boolean to flag what image you current have set as the main image, use that to decide when to switch instead of the images themselves. And Spower, the equals() command is not defined for GreenfootImage, so it uses the default Object implementation, which is the same thing as using ==
SPower SPower

2012/10/15

#
@Builderboy2005 I know the equals method is a method inherited from Object, but if its the same as ==, why does == never work in cases like this? :)
Builderboy2005 Builderboy2005

2012/10/15

#
That is what i am saying, the equals() function will not work :P The reason it works for cases like String is because String class overrides the equals() function to give it better meaning
SPower SPower

2012/10/15

#
O, I understand. Forgot about that.
MatheMagician MatheMagician

2012/10/16

#
Using the boolean idea from builderboy should work.
danpost danpost

2012/10/16

#
I actually prefer to use an int which controls which element in an array to use. The array can be of String type or of GreenfootImage type.
// instance variables
String[] imageNames = { "square1.png", "square2.png" };
int imageNum;
// control
imageNum = (imageNum + 1) % imageNames.length;
setImage(imageNames[imageNum]);
I guess the reason I prefer this way is that each step is absolute processing (no decision-making with 'if's or other control flow commands); the control code can be used multiple times to switch the image back and forth; and, the control code can be put inside a method to be called upon when neccessary (call the method something like 'swapImage'). 'imageNum' could be initially set to -1 and the method called to initially set the image.
SPower SPower

2012/10/17

#
Just came up with this:
if (ImageVisitor.equal(getImage(), new GreenfootImage("square1.png") ) )
The ImageVisitor is part of the greenfoot package btw, so you don't need additional work to get access to it.
There are more replies on the next page.
1
2