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

2013/6/27

getting a tower to attack

darkmist002 darkmist002

2013/6/27

#
I'm having a hard time getting a tower to attack spawned units. Trying to get it to attack a unit when it sees it in a certain radius, but keeps giving me an index out of bounds error for it, so it stops the game. here's the coding for the tower i have:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class fasttower here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class fasttower extends tower
{
    /**
     * Act - do whatever the fasttower wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //att lv 1/2/3          1/2/4/8
    //att spd lv 1/2/3      1/.75/.5/.35
    //price                 5
    //upgrade price         10
    
    private int attack = 1;
    private long attspd = 1000; //milliseconds
    private int range = 5;
    private int lvl = 1;
    private long count = 0;
    private long aTime = 0;
    private int wave = 1; //have wave class come in as well
    
    public void act() 
    {
        // Add your action code here.
        //use getObjectsInRange() method for actors to make a radius for towers
        update();
        attack();
    }   
    
    private void attack()
    {
        List<enemies> e = getObjectsInRange(5, enemies.class);//change radius to variable for when
                                                            //upgrading to up radius.
         
        if(e !=null)
        {
            /** attack first enemy.
             * if(clock)
             * {
              *     e(0).health - attack;
              *}
              *
            **/
            //int index = e.indexOf(enemies.class);
            enemies temp = e.get(0);  
            double hp = temp.getHealth();
            if(clock())
            {
                hp -= attack;
            }
        
            if(hp <= 0)
            {
                ((tdpath)getWorld()).removeObject(temp);
                // minerals+= whatever the value is for the monster.
            }
        }
    }
    private void update()
    {
        /**if have minerals and click on tower, upgrade to next lv
         * will have to add a minerals class and have it seen in every class.
          *if(minerals == 10 && clickontower && (lv <= 4))
          *{
          *     lvl++;
          *     attack += (attack*lvl);
          *     attspd = attspd - 200;
          *     range += (range*lvl);
          *}
          **/
        
    }
    
    //says when to attack based on milliseconds (1000 = 1 sec, so every second for this tower unless
    //it's upgraded)
    private boolean clock()
    {
        long time = System.currentTimeMillis();
        
        if(count!=0)
        {
            long temp = time - count;
            aTime = temp;
        }
        count = time;
        
        if(aTime >= 1000 && lvl == 1)
        {
            aTime = 0;
            return true;
        }
        
        if(aTime >= (1000 -(lvl*100)) && lvl > 1)
        {
            aTime = 0;
            return true;
        }
        return false;
    }

}
davmac davmac

2013/6/27

#
Your problem is at line 41. You are checking whether the list returned from getObjectsInRange(...) is null, but that will never be the case as getObjectsInRange(...) never returns null. What it may return is an empty list; it's this condition you should be checking for.
   if (! e.isEmpty()) {

   }
davmac davmac

2013/6/27

#
Oh and by the way, if you are getting an exception, it is quite helpful if you mention which line the exception occurs on! In this case I assume it was line 51.
darkmist002 darkmist002

2013/6/27

#
that fixed it, thanks. now i have to figure out how to get the tower to take away health from the spawned unit's class (that's where the health is located) so the attacks actually do damage. also have to figure out how to make it where clicking on the buttons for the towers lets you place them on the field where you want and then detracts minerals from the total minerals. scenario with tower code added: http://www.greenfoot.org/scenarios/8919
You need to login to post a reply.