I want to make a method where an Actor spawns itself with something like getWorld().addObject(this, x, y);
but I can't do that since this is an object not an Actor. How do I do this (don't say new Actor() because that is not what I want to do)?
/** * Creates and adds a new actor of the specified class into the world at the given coordinates. * * @param cls the class from which the new actor is created * @param xLoc the x-coordinate at which to place the object into the world * @param yLoc the y-coordinate at which to place the object into the world */ private Actor addActor(Class cls, int xLoc, int yLoc) { Actor actor=null; try { actor=(Actor)cls.newInstance(); } catch(Exception e) { } if (actor!=null) getWorld().addObject(actor, xLoc, yLoc); return actor; } // to spawn an object of the same type as Actor 'this', use: addActor(this.getClass(), x, y); // if you need a reference to the newly created actor, use: Actor actor = addActor(this.getClass(), x, y); // if your class is Zombie and you need the new instance type cast to that: Zombie zombie = (Zombie)addActor(Zombie.class, x, y);
Zombie zombie = new Zombie(); getWorld().addObject(zombie, x, y);
BufferedReader br = new BufferedReader(new FileReader("levels/" + levelName + ".txt")); String line; while((line = br.readLine()) != null) { List<String> items = Arrays.asList(line.split("\\s*,\\s*")); int item0 = Integer.parseInt(items.get(0)); int item1 = Integer.parseInt(items.get(1)); int item2 = Integer.parseInt(items.get(2)); int item3 = Integer.parseInt(items.get(3)); Actor actor = (Actor) Object.objectList[item0]; getWorld().addObject(actor, item1, item2); }
public void addedToWorld(World world) { BufferedReader br = new BufferedReader(new FileReader("levels/"+levelName+".txt")); while ((line = br.readLine()) != null) { List<String> items = Arrays.asList(line.split("\\s*, \\s*")); int item0 = Integer.parseInt(items.get(0)); int item1 = Integer.parseInt(items.get(1)); int item2 = Integer.parseInt(items.get(2)); int item3 = Integer.parseInt(items.get(3)); Actor actor = (Actor)Object.objectList[item0]; world.addObject(actor,, item1, item2); } br.close; }