Duplicating entity when colliding (sOLVED)


(christopf) #1

I’ve searched the forum now for some time but i didnt find a solution or hint that pushed me more into a direction so i ask you again for help wise beings.

this time i want to create a new entity (or instance?) of an entity class when colliding with the hero. actually if collided the hero becomes the new (duplicated) entity and gets recycled. i created some set/get functions to get the timing right and to know which class the collided entity belongs to but i dont know how to generate entities based of this class. i tried with the create function in my world class but what i got were those two errors:

with just (create(Leutnan,true)); in world update the game starts but this error occurs when my hero gets recycled:

[Fault] exception, information=ArgumentError: Error #1063: Argument count mismatch on Leutnan(). Expected 2, got 0.

i guess this is because my leutnan constructor expects two numbers (x,y). when i go with this

(create(Leutnan(mori.transPoint.x, mori.transPoint.y), true));

(or simple numbers) it throws out this:

col: 56 Error: Incorrect number of arguments.  Expected no more than 1. (create(Leutnan(mori.transPoint.x, mori.transPoint.y), true));`

col: 13 Error: Implicit coercion of a value of type Leutnan to an unrelated type Class (create(Leutnan(mori.transPoint.x, mori.transPoint.y), true));

Additionally i wonder if i have to precreate an array of variables or names for the “duplicated” entities or can FP do this for me? After added to the world i need to add some behavior to the entity as well.

Or is this too much to ask?

Can you give me a hint in which direction i need to dig?


(Bora Kasap) #2

i think you can use a little public function inside your entity to call it if it is a copy next to your “create” function.

aCopyOf(original:Entity):void
{
this.x = original.x;
this.y = original.y;
this.something = original.something;
}

so you can create it from orginal entity like that

if(this.collideHero etc...)
{ 
   Leutnan(this.world.create(Leutnan)).aCopyOf(this); 
}

(Jacob Albano) #3

Another approach I’ve seen on here a few times is to have a function called setup() that, well, sets up certain variables.


public function MyEntity(x:Number = 0, y:Number = 0)
{
    setup(x, y);
}

public void setup(x:Number = 0, y:Number = 0):void
{
    this.x = x;
    this.y = y;
}

The underlying issue is that Flashpunk tries to construct your class without passing any parameters, and due to limitations in as3 we can’t actually add that functionality. If you set default values for your constructor’s parameters, it can be called without passing anything in and no error is raised.

Using setup() internally allows you to simply move your setup code to a point where it can be called on an existing item, like when you create it with create(). You would use it like so:

MyEntity(world.create(MyEntity, true)).setup(50, 100);

(christopf) #4

Thank you both for you replying. i tried it the way you suggested in the enemy class, the hero class and the world class, but i didnt work once. each time it throws the same error with the 2 missing arguments :confused:
maybe i’m doing it totally wrong

REWIND it works…forgot the zeros in the parenthesis :x


(Jacob Albano) #5

Yep, the default parameters are the key to making it work. It allows the runtime to treat it as though the constructor takes no parameters at all.


(christopf) #6

Finally after getting the camera moving i want now to add some behavior to newly created entity (if that is possible alltogether)

for this i got a function that is generating random strings and with the setup(x,y) function extended by nahme for the entities name i managed to get every new entity a special name. but i dont know how to access this entity by its name and how to threat it like the other entities i drop in beginning with add(AndADifferentName?) :smiley: to check i used getInstance(nahme) and a fixed string name but this drops only the class name of the entity (addiotinally with the randomly generated name i can acces it this way)

i dont know how to describe it any better. anybody thinking thats a good way to accomplish this? have you maybe a hint for another direction? is this bad way to code? and is it possible in any way?

thanks so far


(christopf) #7

I think i nearly got it.
I moved all the behavior() stuff directly in the mother class of all enemy entities and it seems to work fairly good. but i cant test it properly because FP.distance throws an error when my hero entity gets recycled.
(for getting the hero into the enemy mother class i use:

var moir:Mori = world.getInstance("mori") as Mori;

maybe thats the crux of the matter since it drops a null if it cant find a “mori” instance…i got to check this


(Bora Kasap) #8

so, add one more condition for checking an object and using it… i mean, if you’re checking distance between an object and anotherobject, you just do that

if (anotherobject != null) FP.distance(this, anotherobject);