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

2022/2/21

Area Damage Help

s1ice s1ice

2022/2/21

#
I've been working on a tower defense game, and I seem to be having trouble while working on area damage. I have a method in the enemy class that takes away health, and I want to call it from every enemy in range to a specific tower. enemy class method:
public void hurt(int damage)
tell me if you need more code from the enemy class attempt at area damage in tower class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 *                                          WIP
 */
public class Crusher extends Tower
{
    public static int cost = 25;
    public Crusher(){
        super();//i dont think u have to worry about this, tell me if u need the Tower class code
        this.damage = 2;
        this.range = 75;//in pixels (might change it idk)
        this.attackSpeed = 4;
    }
    int time = (int)(attackSpeed*100);
    boolean spend = true;
    public void act()
    {
        /** only can be placed on path. 
         *  works like crusher from clash of clans builder base */
            time--;
            List <Enemy> prox = getObjectsInRange(range, Enemy.class);
            if(time < attackSpeed){
                Greenfoot.playSound("smash.mp3");
                for(Enemy pie : prox){
                    pie.hurt(damage);
                }
                time = (int)(attackSpeed*100);
            }
        }
    }
}
I don't know how to explain it but it just doesn't work It's supposed to hurt all enemies within the range every 2 seconds or so (however long the time variable takes to reach attackSpeed) it doesn't seem to do that
danpost danpost

2022/2/21

#
s1ice wrote...
tell me if you need more code from the enemy class
It might be best to show all the Enemy class codes also.
s1ice s1ice

2022/2/22

#
Nevermind, I fixed the issue by using a for loop:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 *                     CHECK THE GREENFOOT FORUM POST
 */
public class Crusher extends Tower
{
    public static int cost = 125;
    public static int range = 75;
    public Crusher(){
        super();
        this.damage = 2;
        this.range = 75;//in pixels (might change it idk)
        this.attackSpeed = 2;//in seconds
        this.projectileType = 1;//one thru four, idk what they'll be yet
        setImage("crusher.png");
        getImage().scale(75,75);
    }
    int time = (int)(attackSpeed*100);
    boolean spend = true;
    public void act()
    {
        /** only can be placed on path. 
         *  works like crusher from clash of clans builder base */
        if(Map.pause == false){
            if(spend == true) {
                Map.money = Map.money-cost;
                spend = false;
            }
            time--;
            List <Pie> prox = getObjectsInRange(range, Pie.class);
            if(time < attackSpeed){
                for(int i = 0 ; i < prox.size(); i++){
                    Pie pie = prox.get(i);
                    pie.hurt(damage);
                }
                Greenfoot.playSound("smash.mp3");
                time = (int)(attackSpeed*100);
            }
            
        }
        
    }
    
}
You need to login to post a reply.