Jump to content

Entity creation on collision


JohnAtreided
 Share

Recommended Posts

Hello!

I'm new to Phaser, and I'm making a game about evolution.

What is the best way to create creatures with properties (speed, color...), knowing that when 2 of these creatures collide, they create a new creature which inherit the properties of their "parents", + some random factor? Later, when this creature will reproduce, it's random factor will be passed to its offspring.

I can't think of a way to retrieve the properties of the "parents" when creating a new creature...

Thank you!

Link to comment
Share on other sites

Hello,

well depends on your set up but it seems as a similar question answere a few hours ago (here).

Anyway if you are colliding two objects you can set collision callback which is basically a function which is called when the two objects (groups) collide. This function is presented two objects and these are the two which are colliding. So if you have a problem with finding your object this way it should be pretty straightforward. Just check some example on collision in phaser docs.

Basically when you use physics.collide you can give it two funcitons to be called which customize your collision (well roughly they do something like that for more I advise you to look through documentation).

Your funciton could look something like this for example (pseudocode ahead!):

function replicate(creature1, creature2)
{
// make a new creature
  yourContext.yourCreatureGroup(yourContext.add(new YourNewCreatureConstructor(creature1, creature2)));
// kill the old ones
creature1.kill();
creature2.kill();
}

As you can guess if you add each creatures properties to it when it's created they will be available here without any further need to look them up.

Either that or you can have some other data set and use pointer or something like that.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...