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 fugufish

i'm trying to create an entity that spawns another entity every X seconds.

plan to put that entity on Weltmeister with two settings:
entity: EntityX
interval: the interval between spawning of EntityX

my code below is not working. I think the variable this.interval somehow does not get passed from Weltmeister to the game.


	EntitySpawner = ig.Entity.extend({
		/* spawns a specified entity every X seconds */
		/* EG: HOW TO USE: in Weltmeister, add Spawner, set entity = EntityUsa_prisoner, interval=2*/
		
		_wmDrawBox: true,
		_wmBoxColor: 'rgba(0, 0, 255, 0.7)',

		type:ig.Entity.TYPE.NONE,
		checkAgainst:ig.Entity.TYPE.BOTH,
		collides:ig.Entity.COLLIDES.NEVER,
		
		speed:0,
		gravityFactor:0,
		spawnPauseTimer:null,		
		size: {x: 16, y: 16},

		init: function( x, y, settings ) {
			if(this.interval){
				this.spawnPauseTimer=new ig.Timer(this.interval);
			}			
			this.parent(x,y,settings);		
		},
		
		update:function(){
			if(this.spawnPauseTimer&&this.spawnPauseTimer.delta()>0){ //finished ticking				
				if(this.entity){
					ig.game.spawnEntity(this.entity,this.pos.x,this.pos.y);
				}				
				this.spawnPauseTimer.set(this.interval);
			}			
			this.parent();
		}
	});

1 decade ago by fugufish

if i replace this.interval with an internal variable (instead of using Weltmeister to set it), then it works. Surely i'm missing something?

1 decade ago by BFresh

I don't see your interval variable defined up top so that it has a scope of the entire entity and can be passed from Weltmeister. I don't see why just adding
interval: 0,

up top wouldn't make it all work!

1 decade ago by fugufish

nope, still doesn't work. the if loop in init:function() is not running somehow!

1 decade ago by fugufish

if i set interval:2 in the code, then yeah it works. But I want Weltmeister to pass the value of 2 (make it flexible to I can set any amount of interval)

1 decade ago by MikeL

Try reversing the order of the this.parent call in init:
        init: function( x, y, settings ) {
            this.parent(x,y,settings);      
            if(this.interval){
                this.spawnPauseTimer=new ig.Timer(this.interval);
            }              
        },

I think it needs to be called first in order to pass the settings set in Weltmeister into your entity.

1 decade ago by fugufish

thanks! works like a charm
Page 1 of 1
« first « previous next › last »