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

2013/1/28

Setting the rotation

moobe moobe

2013/1/28

#
Hi, I have an object which shall turnTowards my Snowman class. I've searched in the web and found this code:
BossLevel bosslevel = (BossLevel) getWorld();
       Snowman snowman = (Snowman) bosslevel.getObjects(Snowman.class).get(0);
       turnTowards(snowman.getX(), snowman.getY());
I don't know why, but the second line gives me a NullPointerException. Isn't the world able to find my Snowman? Or maybe you guys know an other way to get the reference of my Snowman, because that would solve the problem too. But it's not easy, because the objects don't intersect each other..
danpost danpost

2013/1/28

#
I do not think that it is because the world is not able to find you Snowman object. I believe that there is not a world referenced that can find any Snowman object. If 'bosslevel' was not 'null' and had a world object referenced, then you would get a indexOutOfRangeException if the Snowman object was not found, not a nullPointerException. Check to make sure you are only executing this code while the object, whose class the code is in, is in the world. You may have executed some code prior to getting to this code that removed the object from the world. Without seeing the class code in a broader view, it would be impossible to help you in this regard.
moobe moobe

2013/1/28

#
danpost wrote...
You may have executed some code prior to getting to this code that removed the object from the world.
I don't think that this is the problem... I have a "great fireball object" which shall turn to my Snowman object (the code I told you is written in the constructor of the great fireball) which is defenetily in the world. The fireball is created by my "Boss" which is a subclass of my Dragon class.
Without seeing the class code in a broader view, it would be impossible to help you in this regard.
Maybe you can have a look on my code. I can update my program and u can download the source code. As I said: The important classes are "GroßerFeuerball", "Boss" and of course the "BossLevel". Thanks for your help, I'm really thankfull for all of your help!
danpost danpost

2013/1/29

#
If the code is written in the constructor of an object, then that object is not yet in the world. You may want to move that code into an overriden 'public void addedToWorld(World world)' method which is executed one time just after the object is added to the world (see the Actor class API).
moobe moobe

2013/1/29

#
?? I think I have understoof the problem, but I don't know how to solve it. How can I write a constructor in the "addedToWorld" method of Snowman? Isn't there an other way to get snowman's reference?
davmac davmac

2013/1/29

#
You don't write a constructor in a method, and you don't need to write anything in Snowman. What danpost is suggesting is that you move the code that you wrote (in your post) into the 'addedToWorld' method of whatever class it is already in.
moobe moobe

2013/1/29

#
Omg, it works! Thank you very much! :D To be honest, I don't know why, because I'm a noob programmer. What's the different between addedToWorld and the constructor?
danpost danpost

2013/1/29

#
The constructor executes once for each object before ever being placed into the world. The addedToWorld method executes whenever the object is (and after being) put into the world (this may happen more than once in the life of an object). Both the constructor and the addedToWorld method are executed once prior to any calls to the act method. The addedToWorld method is usually only used when construction of the object cannot be completed without a reference to the world in which the object is to be placed or when other objects need to be added to the world at the same time and are triggered by the adding of this object into the world. There are other cases where the addedToWorld method could be used and I could not possibly account for all of them.
moobe moobe

2013/1/29

#
Ah okay, that's why it says
addedToWorld (World world)  //Writing "World world" gives me the reference to the world.
.
.
.
With that code I get the reference to the world, right?
danpost danpost

2013/1/29

#
Yes. If you wanted to get the width of the world in the method, you would use the following: world.getWidth()
You need to login to post a reply.