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

2014/2/11

how can I check/parse getImage() and compare string

duxbuz duxbuz

2014/2/11

#
Hi I don't know my title has the correct terminology I am trying to run some code ONLY if the image of the object is not already a particular image. I have tried this:
1
2
3
4
5
if(this.getImage().toString() != "Image file name: billgates.png"){
          this.setImage("skull.png");
          Greenfoot.delay(1);
          this.setImage("billgates.png");
      }
and slightly different
1
2
3
4
5
if(this.getImage().toString() != "billgates.png"){
            this.setImage("skull.png");
            Greenfoot.delay(1);
            this.setImage("billgates.png");
        }
ps sorry Bill
davmac davmac

2014/2/11

#
Well, firstly, I wouldn't rely on the image toString() method returning anything really usable. The best way to solve this problem is to store a reference to the image you want to compare against and then compare it directly.
1
2
3
4
5
6
7
8
9
10
11
12
// declare a private field:
private Image billGatesImg;
 
// in your constructor:
billGatesImg = getImage();
 
// and then in your method:
if (getImage() == billGatesImg) {
         this.setImage("skull.png"); 
         Greenfoot.delay(1);  
         this.setImage(billGatesImg); 
}
I'm assuming that "billgates.png" is the default image for the class, you need to adjust the code above if not. As a side note, though, you are doing something very wrong with your initial attempt, which is to compare strings using '=='. You should always compare strings using .equals().
1
if(this.getImage().toString().equals("Image file name: billgates.png")){
See this stackoverflow question.
duxbuz duxbuz

2014/2/11

#
Ok Thanks I dont think I can use this example:
1
2
3
4
5
// declare a private field:
private Image billGatesImg;
 
// in your constructor:
billGatesImg = getImage();
the reason being is that the initial image is not the billgates.png. So do I still have to create an Image type and assign the value billgates.png to it so that I can compare it? If so how then can I assign the path of an image or name of an image to a Type Image?
davmac davmac

2014/2/11

#
Sorry, should have been 'GreenfootImage', not 'Image'!
the reason being is that the initial image is not the billgates.png.
In that case:
1
private GreenfootImage billGatesImg = new GreenfootImage("billgates.png");
You don't need anything in your constructor. The other bit of code is still correct:
1
2
3
4
5
if (getImage() == billGatesImg) { 
         this.setImage("skull.png");   
         Greenfoot.delay(1);    
         this.setImage(billGatesImg);   
duxbuz duxbuz

2014/2/11

#
Thanks for your input. I am now unsure why I cannot get the results I want. Basically it is meant to check if the current image of the leaf object is "billgates.png", which would mean that this leaf has already been eaten, so I wanted it to ignore that leaf and keep moving. This is to check the leaf
1
if(this.getImage() != billGatesImg){
I put my declaration just above the constructor, in the class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Leaf extends Actor
{
    // declare a private field:
    private GreenfootImage billGatesImg = new GreenfootImage("billgates.png"); 
 
    public Leaf()
    {
    }
    
    public void munched(){
        if(this.getImage() != billGatesImg){
            this.setImage("skull.png");
            Greenfoot.delay(1);
            this.setImage("billgates.png");
        }     
    }
}
I call this method from the wombat class
1
2
3
4
5
6
7
Leaf leaf = (Leaf)getOneObjectAtOffset(0, 0, Leaf.class);
        Actor wombat = getOneObjectAtOffset(0, 0, Wombat.class);
        if(leaf != null) {
            // eat the leaf...
        getWorld().removeObject(this);
        //call method it here!!
        leaf.munched();
danpost danpost

2014/2/11

#
Line 14 should be like line 4 of the posted code davmac supplied last.
duxbuz duxbuz

2014/2/11

#
Thanks Would I not be able to use the
1
this.setImage("billgates.png");
? Is this not just the same
duxbuz duxbuz

2014/2/11

#
I see that they are not the same, from the results. And am guessing its to do with objects and instances and memory and pointer locations or something. ha ha. Ok I need to just move my if statement first that might be why my code isn't doing what I expect Thanks
danpost danpost

2014/2/11

#
When using '==' to compare images (or any type object), the two things being compared must be the same instance, not just two instances that are alike.
duxbuz duxbuz

2014/2/11

#
I am currently needing to use the private billGatesImg in another class. So I created a getter getBill()
1
2
3
public GreenfootImage getBill() {
        return billGatesImg;
    }
Seems to be working a little better in that it my wombat does not get eaten by billgates but not quite right yet as my wombat halts. Sure that sounds a bit mad
You need to login to post a reply.