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

2014/3/7

Tower Defense Game Problems

polar568 polar568

2014/3/7

#
I am having a problem when I am trying to target the closest enemy to the tower within a certain radius. I get a NullPointerException at lines 6 in the Arrow constructor and line 4 in the search method. The search method is referenced in the act method of the tower and it is meant to create an Arrow at the towers location when an enemy is within range. The arrow constructor is meant to turn the arrow towards the closest enemy and in the act method of the arrow I have it moving in that direction. Any help would be appreciated!
public Arrow()
    {        
        Ground world = (Ground)getWorld();        
        Btower tower = world.getTower();
        image();
        if(tower.getNearestActor() != null) {
            turnTowards(tower.getNearestActor().getX(), tower.getNearestActor().getY());
        }
    }
public void search()
    {
        if(getNearestActor() != null) {
            getWorld().addObject(new Arrow(), getX(), getY());
        }
    }
danpost danpost

2014/3/7

#
The problem is actually at line 3 of the constructor. The object must be created (the constructor must have completely executed) before the created object can be placed into a world. Therefore, 'getWorld' on line 3 will always return a 'null' value. All your constructor needs to do, in your case, is call 'image'. The rest can be done by the calling 'search' method. I would suspect your 'getNearestActor' method actually returns an Actor object. If so, then (since you already have access to the actor here)
public void search()
{
    Actor actor = getNearestActor();
    if (actor != null)
    {
        Actor arrow = new Arrow();
        getWorld().addObject(arrow, getX(), getY());
        arrow.turnTowards(actor.getX(), actor.getY());
    }
}
polar568 polar568

2014/3/7

#
Thank you so much! That solved the problems!!
polar568 polar568

2014/3/8

#
I have come across another problem, when trying to start a new wave. In my tank class I set the variable deaths to 0. Then I have this code to increase deaths by 1 when a tank is destroyed or touches the edge of the world:
public void getHealth()
    {
        if(health <= 0) {
            Ground world = (Ground)getWorld();
            Counter cash = world.getCounter();
            cash.setValue(cash.getValue() + 15);
            deaths++;
            getWorld().removeObject(this);
        }
    }
public void end()
    {
        if(getX() > getWorld().getWidth()) {
            deaths++;
            Ground world = (Ground)getWorld();
            Counter life = world.getLives();
            life.add(-1);            
            getWorld().removeObject(this);
        }
    }
I also have made a tank that is transparent in the corner of the world to allow these to run even after the tanks moving down the path have all been destroyed. Still though, the deaths variable does not increase. This is the act method:
public void act() 
    {
        GreenfootImage pic = getImage();        
        nextWave();
        transparent();
        gPressed();
        if(pic.getTransparency() != 0) {
            moving();        
            timeStart();
            end();
            getHealth();
            
            
        }
    }
None of the other methods reference deaths.
danpost danpost

2014/3/8

#
How is your 'deaths' field declared?
polar568 polar568

2014/3/8

#
private int deaths = 0;
danpost danpost

2014/3/8

#
Please refer to my Overview: Subclassing in Greenfoot scenario. Take particular interest in the 'FIELDS' section of the world class.
polar568 polar568

2014/3/8

#
Thank you! I just didn't fully understand the static label.
You need to login to post a reply.