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

2013/12/22

How to make boolean stay true

bzhang bzhang

2013/12/22

#
So here is my code: private boolean countDown = true; private boolean invisible = true; private int counter = 300; private int trans = 0; GreenfootImage img = new GreenfootImage("beeper.png"); public Mine() { } public void act() { explode(); } public void explode() { if(invisible){ img.setTransparency(trans); }else{ img.setTransparency(255); } Actor p = getOneObjectAtOffset(0, 0, Pacman.class); if(p != null){ countDown = true; invisible = false; } if(countDown){ counter = counter - 1; } if(counter == 0){ getWorld().addObject(new Explosion(), getX(), getY()); getWorld().removeObject(this); } } I want to make it so that the countDown boolean stays true even after the mine and the pacman aren't touching anymore. Help please :(
bzhang bzhang

2013/12/22

#
I also want the invisible boolean to stay true afterwards
SPower SPower

2013/12/22

#
You don't seem to change the countDown to false, so it will stay true.
bzhang bzhang

2013/12/22

#
Thank you, I just realize that this code works, but when I add the mine to the world, it is visible even though I set the transparency to 0.
danpost danpost

2013/12/22

#
I do not see where the countdown boolean is ever false to begin with.
bzhang bzhang

2013/12/22

#
Sorry, I put the wrong code. Here is what I actually have: private boolean countDown = false; private boolean invisible = true; private int counter = 300; private int trans = 0; GreenfootImage img = new GreenfootImage("beeper.png"); public Mine() { } public void act() { explode(); } public void explode() { if(invisible){ img.setTransparency(trans); }else{ img.setTransparency(255); } Actor p = getOneObjectAtOffset(0, 0, Pacman.class); if(p != null){ countDown = true; invisible = false; } if(countDown){ counter = counter - 1; } if(counter == 0){ getWorld().addObject(new Explosion(), getX(), getY()); getWorld().removeObject(this); } }
danpost danpost

2013/12/22

#
However, you are using a lot of fields for which one should be sufficient.
private int counter = 0;

public Mine()
{
    getImage().setTransparency(0);
}

public void act()
{
    if (counter > 0)
    {
        counter--;
        if (counter == 0)
        {
            getWorld().addObject(new Explosion(), getX(), getY());
            getWorld().removeObject(this);
        }
        return;
    }
    Actor p = getOneObjectAtOffset(0, 0, Pacman.class);
    if (p != null)
    {
        counter = 300;
        getImage().setTransparency(255);
    }
}
bzhang bzhang

2013/12/22

#
Thank you very much, I could use a lesson in being more efficient with my code
danpost danpost

2013/12/22

#
Let me see if I can help:
private int counter = 0; // obviously, you will need this

// the constructor is used to set up initial state of actor being constructed
public Mine()
{
    getImage().setTransparency(0); // hide image (this is an initial state of the object)
}

// the act method is for code that needs to run on a continuous basis
public void act()
{
    // running the counter
    if (counter > 0) // has counter been started
    {
        counter--; // counter tick
        if (counter == 0) // is counting finished
        {
            getWorld().addObject(new Explosion(), getX(), getY());
            getWorld().removeObject(this);
        }
        return;
    }
    // if here, counter has not been started yet
    // check to start counter
    if (getOneObjectAtOffset(0, 0, Pacman.class) != null) // is triggered
    {
        counter = 300; // start counter
        getImage().setTransparency(255); // show image
    }
}
This could be broken down and made to look a bit more readable in the act method using this:
public void act()
{
    if (timerRunning()) runTimer(); else checkForTrigger();
}

private boolean timerRunning()
{
    return counter > 0;
}

private void runTimer()
{
    counter--;
    if (timerElapsed()) explode();
}

private boolean timerElapsed()
{
    return counter == 0;
}

private void explode()
{
    getWorld().addObject(new Explosion(), getX(), getY());
    getWorld().removeObject(this);
}

private void checkForTrigger()
{
    if (foundTrigger()) activate();
}

private boolean foundTrigger()
{
    return getOneObjectAtOffset(0, 0, Pacman.class) != null;
}

private void activate()
{
    counter = 300;
    getImage().setTransparency(255);
}
You need to login to post a reply.