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

2013/12/21

Keeping track of a class that is not extending me?

forteddyt forteddyt

2013/12/21

#
Hello! I am programming a Bomberman and I need to keep track of when I (BomberManP1) place a bomb (Bomb). I need this because I am making an upgrade system where crates drop that have upgrades, and the first upgrade I'm trying to implement is BombUp (Increase number of bombs I can place). I have it set up now so that, after I check if I can place a Bomb, I place one, and increase my "numOfBombs" int by 1. I need now to set up a way to decrease "numOfBombs" int by 1 after the Bomb explodes, without having a variable in my BomberManP1 class that just counts down the same amount of time I have my Bomb count down from. (Sorry if this is confusing, if you need clarification please tell me). Now, the code - BomberManP1 act()
public void act() 
    {
        // Add your action code here.
        updateSelf();
        time++;
        moveTimer--;
        bombDelay--;
        bomb();
        move();
    }
BomberManP1 updateSelf()
public void updateSelf()
    {
        if(getWorld().getObjectsAt(getX(), getY(), DeadlyExplosion.class).size() != 0) // Ignore this bit, work in progress.
        {
            setImage("Bomberman Death P1.png");
            setImage("Bomberman Death P2.png");
            setImage("Bomberman Death P3.png");
            setImage("Bomberman Death P4.png"); 
        }
        
        if(getWorld().getObjectsAt(getX(), getY(), BombUp.class).size() != 0 && bombCap < 5) // Don't ignore this bit
        {
            bombCap++;
            getWorld().removeObjects(getWorld().getObjectsAt(getX(), getY(), BombUp.class));
        }
        
    }
BomberManP1 bomb()
public void bomb()
    {
        if(Greenfoot.isKeyDown("space") && getWorld().getObjectsAt(getX(), getY(), Bomb.class).size() ==0  && bombDelay <= 0 
        && numOfBombs < bombCap)
        {
            getWorld().addObject(new Bomb(), getX(), getY());
            numOfBombs++;
            bombDelay = BD_RESET;
            System.out.println("bombCap : " + bombCap + "   isBombCapped : " + isBombCapped + "numOfBombs" + numOfBombs);
        }
    }
Currently the only thing in the Bomb class is it counting down from a set amount of times, progressively changing pictures to make it look as if it's on a fuse, then exploding and adding "DeadlyExplosions" class around it, which kills players and breakable blocks it touches.
danpost danpost

2013/12/21

#
Do you have a superclass, something like 'Player' or 'BomberMan', between Actor and the players to contain all the players within?
forteddyt forteddyt

2013/12/21

#
No, I do not, I have a parent for BomberManP1 (Players - hope to add more players later), a parent for BombUp (PowerUps - hope to add more upgrades), and a parent for UnbreakableWall and BreakableWall (Wall - may implement different types of walls later on). I see what you're getting at though, would that be what you recommend? Or is there any other way?
danpost danpost

2013/12/21

#
Players! good. In the Players class, you should have fields for the following: (1) int to hold maximum number of bombs allowed to have in the world at one time ('maxBombs' ?) (2) int to hold number of bombs currently in the world ('bombCount' ?) Method to be called by bomb when exploding:
public void decrementBombCount()
{
    bombCount--;
}
Then, in the Bomb class, you should have this field (1) Players to hold which player placed that bomb ('player' ?) The Bomb class constructor should be modified to accept a Players parameter, and that constructor should save that argument in its 'player' field. When a bomb explodes, call 'player.decrementBombCount();'. That should control the field as you want.
forteddyt forteddyt

2013/12/21

#
I got you up until the Bomb class modifications, I do not understand what I'm suppose to do, my Players does not take in parameters, and neither does my BomberManP1 and I do not see why they would need to either (if that's what you're going for). Also, Bomb does not extend to BomberManP1, but instead extends directly to Actor. "DeadlyExplosion" extends Bomb. What I added in my bomb class goes as such -
public Bomb()
    {
        boomTimer = BOOM; // Ignore this
        Players player = new BomberManP1();
    }
Then in the boom() (aka the method where the bomb explodes) I added this -
public void boom()
    {
        if ( boomTimer == (BOOM*2/3))
            setImage("Bomberman Bomb P2.png");
        if ( boomTimer == (BOOM/3))
            setImage("Bomberman Bomb P3.png");
        if ( boomTimer == 0 || getWorld().getObjectsAt(getX(), getY(), DeadlyExplosion.class).size() != 0)
        {
            getWorld().addObject(new DeadlyExplosion(), getX(), getY());     // Ignore these. \/
            getWorld().addObject(new DeadlyExplosion(), getX()+1, getY());
            getWorld().addObject(new DeadlyExplosion(), getX()-1, getY());
            getWorld().addObject(new DeadlyExplosion(), getX(), getY()+1);
            getWorld().addObject(new DeadlyExplosion(), getX(), getY()-1);
            player.decrementBombCount(); // Don't ignore this
            getWorld().removeObject(this);
        }
    }
It then gives the error that it can not find variable "player". If I replace "player" with "Players", is gives the error that I can not reference a non-static method decremenetBombCount() in a static context.
shrucis1 shrucis1

2013/12/21

#
forteddyt, I believe what danpost means, is for you to do something like this:
public class Bomb extends //Whatever it extends
{
    Player myPlayer;
    public Bomb(Player placedBy)
    {
        myPlayer = placedBy;
    }

    public void act()
    {
        //Act code here
    }

    public void boom()
    {
        //Called when bomb explodes
        //Explosion Code here
        myPlayer.decrementBombCount();
    }

}
I omitted the code that creates the DeadlyExplosions and determines when the bomb explodes, I was just illustrating what I think danpost means. And when you call the code to create a bomb, do something like this:
public void placeBomb()
{
    getWorld().addObject(new Bomb(this), getX(), getY());
    bombCount ++;
}
With the previous code being in whatever your player class is.
forteddyt forteddyt

2013/12/21

#
I now see what I was suppose to do, however now a whole new slew of issues appeared... The DeadlyExplosion, which extends to Bomb, now throws the exception of DeadlyExplosions constructor not sending a Players type to the Bomb. How do I fix this?
danpost danpost

2013/12/21

#
Sounds like you need a default constructor for the Bomb class as a pass-through for the DeadlyExplosions class. Add to the Bomb class (without replacing code already placed in the class) the following constructor:
public Bomb()
{
}
Truthfully, however, the DeadlyExplosions objects are not really to be considered Bomb objects and the one class should not be subclassing the other.
forteddyt forteddyt

2013/12/21

#
Got it! Thank you both for your help, I really appreciate it.
You need to login to post a reply.