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

2017/2/26

Remove Object Problem

joshuadswa joshuadswa

2017/2/26

#
Hello :) i have some problems here, so i try to follow the code from here http://www.greenfoot.org/topics/58346?offset=0 but its not working .-. here my code for tower
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Tower1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tower1 extends Actor
{
    /**
     * Act - do whatever the Tower1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    boolean weaponStatus = true; 
    int gerak = 1;
    public void act() 
    {
        shootProjectile();
        refreshWeapon();
        gerak++;
     
    }    
    public void shootProjectile()
    {
      List<Minion1> minion = getObjectsInRange(200,Minion1.class);
  
        if(minion.size() > 0 && weaponStatus==true)
        {
            getWorld().addObject(new Projectile1(),getX(),getY());
            weaponStatus = false;
        }
    
    }
    
    public void refreshWeapon(){
        if(gerak%100==0){
            weaponStatus = true;
        }
    }
}
   



for projectile
import greenfoot.*;  
import java.util.List;
/**
 * Write a description of class Projectile1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Projectile1 extends Actor
{
    /**
     * Act - do whatever the Projectile1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
  
    public void act() 
    {
        checkHit();
        checkProjectile();
        hitActor();
    }  
    
    public void checkHit()
    {
        List<Minion1> isi = getWorld().getObjects(Minion1.class);
        if(isi.size()==0)
        {
              getWorld().removeObject(this);
        }
    }
    
    public void checkProjectile()
    {
        Actor miinion = (Minion1)getWorld().getObjects(Minion1.class).get(0);
        turnTowards(miinion.getX(),miinion.getY());
        move(5);
         if(miinion.getWorld()==null)
        {
            getWorld().removeObject(this);
        }

    }
    
    public void hitActor()
    {
         Actor minion;
         minion = getOneObjectAtOffset(0, 0, Minion1.class);
      
         if(minion!=null)
         {
             getWorld().removeObject(minion);
             getWorld().removeObject(this);
         }
    }
    
  
}
    
 

    
Super_Hippo Super_Hippo

2017/2/26

#
Why didn't you reply to the mentioned discussion instead of starting a new one? Well anyways, do you want the projectile to follow a specific actor or should the tower aim at an enemy and the projectile should just move straight in that direction?
joshuadswa joshuadswa

2017/2/26

#
Sorry >.< what i want is, i want to remove some object when another object is removed .-. because when my turret attackin minion, its projectile will remove the minion, BUT in my scenario there's and exception because i remove the minion before the tower's projectile arrive/hit the minion .-.
Super_Hippo Super_Hippo

2017/2/26

#
Couldn't you just remove the projectile after a certain time / after it travelled a given distance / when it hits the edge of the world?
joshuadswa joshuadswa

2017/2/26

#
Nope sir, since my turret's projectile follow the minion (like tower defense) but the minion got removed first before the projectile arrive/hit the minion,
Super_Hippo Super_Hippo

2017/2/26

#
In "normal" tower defense, the projectile (or at least a normal projectile) does not follow the minion. Instead, the tower shoots the projectile in the direction of the target and the projectile just moves forward. However, if you want it to follow a specific minion, pass the target minion as a parameter to the projectile and follow that target as long as the target is in the world. If the target is not in the world anymore, remove the projectile.
joshuadswa joshuadswa

2017/2/26

#
O.O thats exactly what i want , but can you help me about the parameters?
Super_Hippo Super_Hippo

2017/2/26

#
//in tower, when trying to shoot: (shoots nearest target)

int distance=rangeOfTheTower+10, x=getX(), y=getY();
Actor nearestTarget=null;
for (Minion m : getObjectsInRange(rangeOfTheTower, Minion.class)) //if the tower should have infinite range, you can also use getWorld().getObjects(Minion.class)
{
    int dist = Math.sqrt(Math.pow(x-m.getX(),2)+Math.pow(y-m.getY(),2));
    if (dist < distance)
    {
        distance = dist;
        nearestTarget = m;
    }
}

if (nearestTarget != null)
{
    getWorld().addObject(new Projectile(nearestTarget), x, y);
}
//in Projectile
private Actor target = null;

public Projectile(Actor t)
{
    target = t;
}

public void act()
{
    if (target.getWorld() == null)
    {
        getWorld().removeObject(this);
        return;
    }
    turnTowards(target.getX(), target.getY());
    move(5);
    if (intersects(target))
    {
        getWorld().removeObject(target);
        getWorld().removeObject(this);
    }
}
joshuadswa joshuadswa

2017/2/26

#
1 more question sir :) when we use int, whats the diffrent between private int and int?
Super_Hippo Super_Hippo

2017/2/26

#
private/protected/public can only be used outside methods. Only using the type (e.g. int) outside methods is the same as making it public. Inside methods, it is only a temporary variable so you don't have a reference to it anymore after the method or other {} block it is in has ended. Usually, when seeing my codes, if there is no 'private/protected/public before a type, then it should not be outside methods.
joshuadswa joshuadswa

2017/2/26

#
Ok thx a lot sir :) have a nice day :D
You need to login to post a reply.