Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by rootbeerking

Hello again! So I'm slowly learning(thanks to the great help from this forum), and I've gotten quite a bit done on my own now, but once again I've hit a block in the road of programing. My question today has to do with the Trigger Entity. I'm using it along with a modified version of the debris entity(both from the jumpnrun example) to spawn some enemies whenever the trigger is touched. It's working all well and good, but my problem is I only want the enemies to spawn if there are no enemies already spawned from the spawner entity(my modified debris entity)... How would I do this?

Alittle more info of how I&039;m currently doing things: I have a couple spawners that I've named s (example: s1, s2, s3, etc) I've got them all linked to this one trigger entity that, when touched, causes the spawners to spawn one enemy each, in the same spot that I've placed each spawner in the level editor. Basically, I want to make sure that the enemy that was spawned has been killed before the spawner can then spawn another enemy if the trigger is activated again.

As always thank you for taking the time to read this. I look forward to a reply.

1 decade ago by paularmstrong

When you spawn an entity from a spawner, assign it to the spawner. Make sure when your player kills the enemy, you call this.kill(); on your enemy. Then, in your spawner, it's as simple as checking an undocumented variable ;)

if (!this.enemy || this.enemy._killed) {
    this.enemy = ig.game.spawnEntity(EntityEnemy);
}

Hope that helps!

1 decade ago by rootbeerking

Hmm sorry, could you give me more info as to how to do that? I understand the example code but I'm not sure how to actually implement what you're explaining with what I already have.

In my player code to kill enemies I have:
  check: function( other ) {
    if (this.vel.y > 0 && this.pos.y < other.pos.y) {
	    other.kill();
 //etc

And the enemy that the spawner is spawning is in a separate file from the spawner...

Again, sorry for having to ask for more explaining... I'm very new to all this still.

1 decade ago by paularmstrong

Yep, you're on the right track.

Your player kills the enemy–sounds right!

Now, I assume that your spawner is an entity in itself. For this example, I'll use "EntityEnemySpawner".

When your trigger entity calls the EntityEnemySpawner's triggeredBy method, that's where you drop in the check and assignment. Here's what that spawner should look like:

ig.module('game.entities.enemy-spawner')
.requires(
    'impact.entity',
    'game.entities.enemy'
)
.defines(function () {

    EntityEnemySpawner = ig.Entity.extend({

        _wmScalable: true,
        _wmDrawBox: true,
        _wmBoxColor: 'rgba(196, 255, 0, 0.7)',

        type: ig.Entity.TYPE.NONE,
        collides: ig.Entity.COLLIDES.NEVER,

        enemy: null,

        triggeredBy: function (other) {
            if (!this.enemy || this.enemy._killed) {
                this.enemy = ig.game.spawnEntity(EntityEnemy);
            }
        }

    });

});

Does that make more sense now?

1 decade ago by rootbeerking

Wow that worked brilliantly! Not quite sure why it works though... I mean, how does the game know that only that certain enemy from that certain spawner was the one that was this.enemy?

Thanks for the help!

1 decade ago by paularmstrong

ig.game.spawnEntity() returns the entity that it just spawned, so by saving a reference to that entity on `this` (the spawner), it will always know about the correct one.

1 decade ago by rootbeerking

Ahh, so in other words: magic. Haha, thanks again for the help!
Page 1 of 1
« first « previous next › last »