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

2021/5/28

Getting Information about multiple Images

Total-Eclipse Total-Eclipse

2021/5/28

#
So in my Scenario there are Sensors that change their Image to "Sensor On.png" if they are touching a Block. Now if all Sensors are activated(all Images of the Sensors are "Sensor On.png)) I want to add a Goal. I think this needs to happen in my Scene. Code of the Sensor:
import greenfoot.*;

public class Sensor extends Actor
{
    public void act() 
    {
        if (isTouching(Block.class) == true)
        {
            setImage("Sensor On.png");
        }
        if (isTouching(Block.class) == false)
        {
            setImage("Sensor Off.png");
        }
    }
    public boolean touchingB()
    {
        return isTouching(Block.class);
    }
}
danpost danpost

2021/5/29

#
With references to the images, you can do a comparison:
import greenfoot.*;

public class Sensor extends Actor
{
    static GreenfootImage onImage = new GreenfootImage("Sensor On.png");
    static Greenfootimage offImage = new GreenfootImage("Sensor Off.png");
    
    public Sensor()
    {
        setImage(offImage);
    }
    
    public void act()
    {
        if (isTouching(Block.class) && getImage() == offImage) setImage(onImage);
        if (!isTouching(Block.class) && getImage() == onImage) setImage(offImage);
    }
}
To check all sensors to be on (in World subclass):
public void act()
{
    boolean allOn = true;
    for (Object obj : getObjects(Sensor.class))
    {
        if (((Actor)obj).getImage().equals(Sensor.offImage))
        {
            allOn = false;
            break;
        }
    }
    if (allOn)
    {
        // do something
    }
}
Total-Eclipse Total-Eclipse

2021/5/31

#
Thank you this helped a lot.
You need to login to post a reply.