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

2014/10/26

addObject in constructor of actor class

getreides getreides

2014/10/26

#
I am currently working on a project, using two actor classes (let's call them classA and classB). The project contains a world class (here: worldclass) with the following constructor...
1
2
3
4
5
public worldclass ()
 
{
     addObject (new classA(), 20, 200);
}
...that works perfectly fine. In classA, the constructor contains...
1
2
3
4
public classA()
{
      getWorld().addObject(new classB(), getX(), getY());
}
...which produces the error "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed." I think I know what it means: this command cannot be executed as the classA-object to which getX()/getY() refers to doesn't exist. However, I've always thought that the constructor of a actor class is executed whenever an object of this class is added to the world. Therefore, the object of classA should exist (as it is inserted via the constructor of the worldclass), and the coordinates should be found by the programm - but obviously it's not working. Moreover, if I replace getX/getY by int-values, the programm states "java.lang.NullPointerException" I am aware that the command getWorld().addObject...does work in classA when executed in any method. Does anyone know why it doesn't in a class constructor (for both error messages)? Thanks in advance
davmac davmac

2014/10/26

#
I've always thought that the constructor of a actor class is executed whenever an object of this class is added to the world.
No, the constructor is executed when the object is created, which is (necessarily) before it has been added to the world. So, while the constructor is executing, you cannot call any method which relies on the object's position in the world - since the object is not yet in the world. Typically, you solve this sort of problem by overriding the 'addedToWorld' method, for example:
1
2
3
4
protected void addedToWorld(World world)
{
    world.addObject(new ClassB(), getX(), getY());
}
danpost danpost

2014/10/26

#
Just to clarify, when using 'addObject(new ActorClassName(), x, y)', calling 'new ActorClassName()' creates the actor AND then returns a reference to the Actor object created that 'addObject' uses to add that actor into the world.
getreides getreides

2014/10/29

#
Hello folks, thank you for your explanation, it does work by using protected void! Is it actually possible to use a loop in this method, in order to create multiple objects of ClassB in one instant (in one act-cycle)? I've tried it out once (I don't have the code right here at the moment), and mabye I did it wrong, but using the loop created only one object...I'll provide you with the code ASAP
Super_Hippo Super_Hippo

2014/10/29

#
Of course it is possible. Maybe you placed them at the same position, so you only saw one?
getreides getreides

2014/11/1

#
Indeed, I used a loop that works, however, I noticed that when I use this code...
1
2
3
4
5
6
7
8
9
10
11
protected void addedToWorld(World world)
{
      int objects = 10;
      Actor class = new ClassB();
     
       while (i < objects)
      {
           getWorld().addObject(class, getX(), getY());
          i++;
      }
}
...my program would only add one object (the constructor of classB makes objects move to a random direction immediately, so I could see for sure that only one object was created). But using this code...
1
2
3
4
5
6
7
8
9
10
protected void addedToWorld(World world)
{
      int i = 0;
      while (i < 10)
      {
          Actor class= new ClassB();
          getWorld().addObject(class, getX(), getY());
          i++;
      }
}
...it works perfectly fine (that is, adding 10 objects at once). Any idea why this nuance makes such a difference?
davmac davmac

2014/11/1

#
In the first version, you create a single actor (line 4) outside the loop, and add it into the world 10 times. Because a single actor can only be in the world once, this has the effect of only adding the actor into the world. In the second version, you create a new actor 10 times - inside the loop! (line 06) - and add it into the world each time. Because they are now separate actors, they can exist independently in the world.
getreides getreides

2014/11/2

#
Now I understand, thank you!
You need to login to post a reply.