Can one object tell other objects in a world to do a certain action?
I've seen programs that must use this, but I cant seem to do it.
Any help?
public class myWorld extends World { public boolean hasToRun = false; public void act() { System.out.println("hasToRun is " + hasToRun); } }
public class actor extends Actor { protected void addedToWorld(World world) { // When this is added to the world, assign hasToRun in myWorld to true. ((myWorld) getWorld()).hasToRun = true; } }
public void die() { ((myWorld) getWorld()).hasToRun = false; getWorld().removeObject(this); }
import java.util.List; import java.util.Iterator;
private void removeActor() { List l = getWorld().getObjects(actor.class); Iterator i = l.iterator(); while(i.hasNext()) { Actor a = (Actor)i.next(); if(a.instanceof actor) { ((actor)a).die(); } } }
public void act() { if(MEETS_PARAMS_TO_REMOVE_ACTOR) { removeActor(); } }
public boolean actorNotHere = true; public actor myActor = new actor();
actorNotHere = getObjects(actor.class).size() == 0; // keeping the value of 'actorNotHere' current
import java.util.List;