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

2018/3/8

run methode once

ZiweZhang ZiweZhang

2018/3/8

#
how can i run this methode once? I want the else to run once, now if i start the game it will keep spamming Wall_red in the game. can someone help me?
if(canSee(Red.class))
        {
            removeObject2(Wall_red.class);
        }else
        {
            World world = getWorld();

            world.addObject(new Wall_red(), wall*7,wall*3);
            world.addObject(new Wall_red(), wall*11,wall*5);
            world.addObject(new Wall_red(), wall*2,wall*7);
            world.addObject(new Wall_red(), wall*8,wall*13);
            world.addObject(new Wall_red(), wall*6,wall*16);
            world.addObject(new Wall_red(), wall*2,wall*22);
            world.addObject(new Wall_red(), wall*8,wall*22);
            world.addObject(new Wall_red(), wall*11,wall*23);
        }
danpost danpost

2018/3/8

#
Add a boolean field to the class:
// field
private boolean seesRed;

// then
if (seesRed != canSee(Red.class))
{
    seesRed = !seesRed;
    if (seesRed)
    // lines 2 through 16 above
}
ZiweZhang ZiweZhang

2018/3/9

#
I've this, but is says "missing return statement".
public class Pressure_red extends PressurePlate
{
    private int wall=35;
    private boolean seesRed;
    public Pressure_red()
    {
        setImage("PressurePlate_red.png");
    }   

    private boolean seesRed()
    {
        if(seesRed != canSee(Red.class))
        {
            seesRed = !seesRed;
            if (seesRed)
            {
                removeObject2(Wall_red.class);
            }else
            {
                World world = getWorld();

                world.addObject(new Wall_red(), wall*7,wall*3);
                world.addObject(new Wall_red(), wall*11,wall*5);
                world.addObject(new Wall_red(), wall*2,wall*7);
                world.addObject(new Wall_red(), wall*8,wall*13);
                world.addObject(new Wall_red(), wall*6,wall*16);
                world.addObject(new Wall_red(), wall*2,wall*22);
                world.addObject(new Wall_red(), wall*8,wall*22);
                world.addObject(new Wall_red(), wall*11,wall*23);
            }
        }
    }
}
danpost danpost

2018/3/9

#
Change line 10 to:
public void act()
You need to login to post a reply.