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

2013/3/28

How to convert from a class/object to an Actor?

1
2
Electrk Electrk

2013/3/28

#
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)?
danpost danpost

2013/3/28

#
This will require several steps as you will see in the code:
/**
 * 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);
In the above, x and y must have pre-assigned values. I used the actual class name in the last example to show that it can be put directly in the call. However, since you know the class already (unless you are in a superclass that extends Actor and spawning an object from one of its subclasses), better would be to use the standard:
Zombie zombie = new Zombie();
getWorld().addObject(zombie, x, y);
Electrk Electrk

2013/3/29

#
This is somewhat unrelated but I don't want to create a new topic and clog up the forums. I get a null pointer exception at line 15:
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);
            }
danpost danpost

2013/3/29

#
Why is this code being executed from an Actor subclass? And if executed from a constructor of the class, then 'getWorld' will return 'null' as the object is not yet in the world. Move the code to an 'addedToWorld' method which executes when the object is placed in a world:
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;
}
You do not have to call this method from anywhere. It is automatically executed when the object of this class is placed in a world ('getWorld' will now return the world in which the object was placed). Two things, why is there an extra field read in that is not being used? and why is this code in an actor class?
Electrk Electrk

2013/3/29

#
danpost wrote...
Two things, why is there an extra field read in that is not being used?
I'm going to work on that later
danpost wrote...
and why is this code in an actor class?
Because when I call World.addObject when Level Reader is its own class and does not extend anything says it cannot be referenced from a static context. I don't know how to fix this because my LevelReader class is not static. I also get an out of memory heap space exception.
danpost danpost

2013/3/29

#
OK. Here is what should be happening. Your world sub-class should create a new LevelReader object,, giving it a level name in its parameter so it knows which file to read from. The data for the world layout for that level should be read in and saved in field(s) in the LevelReader class and excessible to other classes (or your world subclass) through method calls (reading in of the data and storing that data in fields should be done in the LevelReader class constructor). Your world subclass should retrieve the data through those methods and build the world. Oh, and also, being that the LevelReader class does not extend Actor or a subclass of Actor, using the 'addedToWorld' method is obviously out of the question.
danpost danpost

2013/3/29

#
Electrk wrote...
Because when I call World.addObject when Level Reader is its own class and does not extend anything says it cannot be referenced from a static context. I don't know how to fix this because my LevelReader class is not static.
The problem here is that with 'World.addObject' you are trying to add an actor object to a class. You need to add the actor object to a specific world object. The message was saying that 'World' was being referenced from a static context (not 'LevelReader'), meaning the non-static method 'addObject" was coded to be executed on the class 'World'.
Electrk Electrk

2013/3/30

#
let me try it
Electrk Electrk

2013/3/30

#
It says addActor cannot be referenced from a static context when I try to call it from my World subclass
danpost danpost

2013/3/30

#
addObject, not 'addActor'.
Electrk Electrk

2013/3/30

#
I added the addActor method and tried to call it from my World subclass
danpost danpost

2013/3/30

#
Sounds like you are still trying to run methods on classes (by using the class name) instead of on objects created from those classes.
danpost danpost

2013/3/30

#
Electrk wrote...
but I can't do that since this is an object not an Actor.
What did you mean by this statement in your initial post in this discussion? Please elaborate and be specific as to the classes and objects involved. Using the names can help.
Electrk Electrk

2013/3/31

#
I'm not really sure but I got what I was trying to do working. Thank you so much for your help. Sorry for being irritating...
danpost danpost

2013/3/31

#
How come the latest posts are showing posting times of negative minutes ago?
There are more replies on the next page.
1
2