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

2013/7/17

special attack types

darkmist002 darkmist002

2013/7/17

#
2 of the towers i want to work with special attacks (one for slowing down units when they are hit, and the other to do splash damage). for the splash damage one i figured if i get all the units in a range and damage them then that would work, but it won't let me cast an array, so should i make a loop in there that takes each non empty value of the array and damage it individually, or is there a way to attack them all at once? the way the units spawn now i have the range set to hit 2 of them, and i want it to do damage to them when the attack hits one of them and is removed. here's the coding i have for the attack:
public class splashDamage extends towerAttacks
{
    /**
     * Act - do whatever the splashDamage wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    //damage        2/4
    //att spd       1/.75 sec
    //aoe radius    2 units/3 units (space wise)
    int x, y;
    enemies e;
    int damage;
    
    public splashDamage(enemies E, int Damage)
    {
        e = E;
        damage = Damage;
    }
    
    public void act() 
    {
        // Add your action code here.
        if(isNotThere())
        {
             getWorld().removeObject(this);
        }
        else
        {
            x = e.getX();
            y = e.getY();
            moveTo(x, y);
            isHit();
        }
    }    
    
        private void moveTo(int x, int y)
    {
        double xX = x - getX();
        double yY = y - getY();
        double angle = Math.atan2(yY, xX)*180/Math.PI;
        setRotation( (int) angle);
        move(1);
    }
    
    public void isHit()
    {
//         if(getOneObjectAtOffset(0, 0, enemies.class) != null)
//         {
//             //do attack damage
//             enemies a = (enemies) getOneObjectAtOffset(0, 0, enemies.class);
//             a.decHealth(damage);
//             getWorld().removeObject(this);
//         }
        if(!getObjectsInRange(70, enemies.class).isEmpty())
        {
            enemies a = (enemies) getObjectsInRange(70, enemies.class);
            a.decHealth(damage);
            getWorld().removeObject(this);
        }
        else if(getX() <=0 || getX() >= getWorld().getWidth() || getY() <= 0 || getY() >= getWorld().getHeight())
        {
            getWorld().removeObject(this);
        }
    }
    
    public boolean isNotThere()
    {
        if(e.getWorld() == null)
        {
            return true;
        }
        return false;
    }
}
the commented out part is that original attack that only hits one. as for the slowing one, since the units that spawn have a different move method then the regular move that you can add an int to, not sure how to slow them down for a few seconds.
Gevater_Tod4711 Gevater_Tod4711

2013/7/17

#
To attack all the enemys in the range you need to use a loop and attack them all at once. The code for this could look like this:
    public void isHit() {
        java.util.List<enemies> enemiesInRange = getObjectsInRange(70, enemies.class);
        if(!enemiesInRange.isEmpty()) {
            for (int i = 0;  i < enemiesInRange.size(); i++) {
                enemiesInRange.get(i).decHealth(damage);  
            }
            getWorld().removeObject(this);  
        }  
        else if(getX() <=0 || getX() >= getWorld().getWidth() || getY() <= 0 || getY() >= getWorld().getHeight())  
        {  
            getWorld().removeObject(this);  
        }  
    }
darkmist002 darkmist002

2013/7/17

#
ok, i'll try it. was hesitant to use a loop in greenfoot since i'd read about it causing other things not to work till the loop was done, but if it does what i want then that'll be fine for me.
danpost danpost

2013/7/17

#
As far as loops and 'causing other things not to work till the loop was done': It is true that nothing other than the loop will execute until the loop has been exited. This just means that you use them when you want to accomplish something (or a set of somethings) during one act cycle. If you are trying to create animation or have things occur at separate times, then a loop is not what you want to use. In this case, you utilize the act cycle itself as the looping agent (doing each step of the loop on different act cycles; and using an int field (usually referred to as a counter or timer) as a speed regulator if it is not a one-to-one on action and cycle).
darkmist002 darkmist002

2013/7/18

#
thanks, was hesitant about using loops b/c of what i had read. glad i asked you guys about it, otherwise i won't have even tried loops.
You need to login to post a reply.