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

2013/1/18

Destructor

1
2
moobe moobe

2013/1/18

#
Hi, is there a destructor in Greenfoot? If not, I'd like to write a method which is called only then, when my actor dies. How is that possible?
danpost danpost

2013/1/18

#
There is a 'removeObject' method in the World class API; maybe that is what you need.
moobe moobe

2013/1/18

#
Nope, I mean something like: if (objectRemoved) { ... } Is that possible?
danpost danpost

2013/1/18

#
if (getWorld() == null)
will return 'true' if the object has been removed from the world.
moobe moobe

2013/1/18

#
Thank you so much!! :D
moobe moobe

2013/1/18

#
UPDATE: I'm sorry, but this doesn't work. I've written:
if (getWorld() == null)         
        {
            life--;
            reanimation();
        }

//In reanimation it says:

Greenfoot.setWorld(new Level1(this, level));
The problem is that he doesn't execute the method, maybe because he's dead. The fireball is the actor which kills him, but he has no reference on snowman, so he isn't able to execute reanimation(). What would you do?^^
danpost danpost

2013/1/18

#
Please show the whole class, as the problem does not reside in the code you have given.
moobe moobe

2013/1/18

#
Hmm, I think that's quite difficult, but you could have a look on my game, I published the source code now... Edit: Don't care about my language mistakes like "hitten", it's written in "Denglish" :)
danpost danpost

2013/1/19

#
Is your 'reanimate' method missing code for a couple levels?
moobe moobe

2013/1/19

#
Yes, but it´s impossible to die in the first two levels. :)
Gevater_Tod4711 Gevater_Tod4711

2013/1/19

#
If you are talking about a destructor method like there is one in C++ for example there is the method finalize in Java. This method is called every time the Java Garbage Colector destroys an object. But this method is not called in every case. There are VM's that don't call this method. Here is a link to the API. Maybe this is what you are searching for.
moobe moobe

2013/1/19

#
And how can I implement that in my game now?
moobe moobe

2013/1/19

#
I have a new idea - My program would work if it would be possible for my fireball to get Reference of the Snowman. But I think this won't be easy again, so the "Destructor" like the one in C++ is the better solution. Beside the problem with the "destructor", I have found an other bug. When the vertical speed becomes too high, my Snowman falls "through the ground". Is it possible to solve this?
danpost danpost

2013/1/19

#
The main problem is that your snowman does not really know what level he is on. The 'level' value in the world class is not the same one as in the snowman's class. It may just be a matter of passing the level number into the constructor of the Snowman and having the constructor copy the value.
Gevater_Tod4711 Gevater_Tod4711

2013/1/19

#
To your first problem: Well the fireball probably should only get the reference to your snowman if it hit's him. In this case it would be easy to get it's reference. Just use the method getOneIntersectingObject. If it doesn't hit the snowman it would be more difficult. To your second problem: It most probably is possible but not without knowledge of your source code.
There are more replies on the next page.
1
2