RESOLVED: How to respawn an entity after it is removed?


(John Heigns) #1

Hi,

I am attempting to recreate Pong in Flashpunk (very original, I know) for practice, and I was able to remove the ball whenever it reaches either side of the screen. What I am wondering is what is the best way to reset the ball after it is removed?


(TaylorAnderson) #2

Every entity has a removed() function you can override. This is called (as you might expect) right before the entity is removed from the world. You can call world.add(new Ball) in that function.


(John Heigns) #3

When I try that, I recieve this error:

[Fault] exception, information=TypeError: Error #1007: Instantiation attempted on a non-constructor.

I guess I can only spawn in the ball in my world class. How should I do that in these circumstances?


(Justin Wolf) #4
override public function removed():void
{
     world.add(new Ball);
     super.removed();
}

Every Entity has a world property you can access that returns the world it currently belongs to. Referring to it, you can then create/add new Entities from inside others. Or you could just get the current world by doing FP.world.

Edit: Just now realizing TTL_Anderson practically explained it the same way! Didn’t read through any of the replies, just went straight to replying myself.


(TaylorAnderson) #5

true! okay, so since theres just one ball on the screen at one time, hold a reference to it in your gameworld. then, in your gameworld’s update() function, put:

if (!ball.world)
{
     world.add(ball = new Ball)
}

that should work…


(John Heigns) #6

Thanks so much! That worked completely, I had already tried the removed() function but had missed the super.removed() part.


(Justin Wolf) #7

The super() shouldn’t have fixed anything, though. Try removing it and it should still work. The removed() function doesn’t actually do anything at all in the Entity class itself (which is what super() does - it calls all of the base function from your override, and since there is nothing actually contained in the base removed() function, super() isn’t necessary). It’s simply called every time the Entity is removed from the world and allows the user to override it just in case they want something to occur when in fact the Entity is removed from the world.

Your error is likely referring to the actual creation of your Ball and probably some sort of typo you had when attempting to create it.


(Jacob Albano) #8

That error was probably caused by something like this:

world.add(ball = new ball);

using the variable name instead of the class.

In the future, @JohnHeigns, it really helps if you show the specific line of code that’s causing the error. Makes it easier for us to help out when we don’t have to guess what your problem is.