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

2021/3/10

Greenfoot won't recognize same image

AnJoMorto AnJoMorto

2021/3/10

#
I have this piece of code that should be quite logical but Greenfoot won't recognize that the image is the same:
private GreenfootImage[] images = {new GreenfootImage("chestBleu.png"), new GreenfootImage("chestBleuOpen.png")};

    public void act()
    {
    
        if(  this.isTouching(Player.class) && Greenfoot.mousePressed(this))
        {
            
            System.out.println(this.getImage().toString() + " vs " + images[0].toString());
            
            if (this.getImage() == images[0])
            {
                System.out.println("correct");
                setImage(images[1]);
               //...

            }
            else {
                System.out.println("why this?");
                //...

            }
            
        }
    
    }

}
Here's what the console shows :
 Image file name: chestBleu.png   Image url: [...] vs Image file name: chestBleu.png   Image url: [same as the other one]
why this?
I've alredy try to make a getImage().toString().equalsIgnoreCase but it didn't work either... Thank you for your time
RcCookie RcCookie

2021/3/10

#
There are two types of equality in java: - Same instance (==): Checks weather the two compared objects are referring to the same instance of a class. Only primitives (int, long, boolean…) can be logically compared with this as they work differently and aren’t instances of Object. - Is equal(someInstance.equals(someOtherInstance)): Checks weather two instances are logically equal to each other while not necessarily referring to the same instance. The equals method is defined in Object, the root class of any java class, so every class has this method (except primitives). The default implementation inside of object simply uses the instance check == to compare to the other object. Subclasses can however override this implementation to define when another object should be considered to be equal to it. This is for example the case in GreenfootImage. In your case you are checking for instance equality which is obviously not the case as you create multiple GreenfootImages simply from the same file. You should use the logical equality check here, for example like this:
if(imageA.equals(imageB)) {
    // …
}
One thing to watch out for is the fact that equals is called on an instance, in this case imageA. If that is null, the statement will throw a NullPointerException. To prevent this and still don’t have to check against null every time there is a handy method in Objects, which can be used like this:
import java.util.Objects;

// —- in method —-

if(Objects.equals(imageA, imageB)) {
    // …
}
This will check for null and not throw an Exception. If both values are null, it will also return true.
danpost danpost

2021/3/10

#
You probably are using a default image of a chestBleu.png for the class, which is NOT the same instance of the first image in images. Add a constructor to the class with the following line:
setImage(images[0]);
danpost danpost

2021/3/10

#
Another, and more simple way around this, is by changing line 11 to:
if (this.getImage() != images[1])
which avoids comparison with default image.
AnJoMorto AnJoMorto

2021/3/10

#
RcCookie wrote...
There are two types of equality in java:
Thank you for your awesome explanation. It made clear some things I've done by the past that ended up not working that I could try to redo now. BUT in this situation it hasn't worked... I tried with both of the codes you gave me and still the console shows the same result... maybe my image is just haunted
AnJoMorto AnJoMorto

2021/3/10

#
danpost wrote...
You probably are using a default image of a chestBleu.png for the class, which is NOT the same instance of the first image in images.
*facepalm* that's exactly it... Oh well thank you for your help I really started to think that nothing could be done here
You need to login to post a reply.