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 blueicelt

I am adding a small messaging notification to my game and I have run into an issue. It appears that all entity timers and alpha settings are global to that type of entity and not to each individually spawned entity. When I decrease the timers and alphas on an entity to fade it out, all of them fade out at the same time.

EntityMessage = ig.Entity.extend({
        collides: ig.Entity.COLLIDES.NEVER,
	font: new ig.Font('media/font.png'),
	size: {x:300, y:50},
	zIndex: 40,
	timer: 300,
	gravityFactor: 0,
	message: '',
	previousMessage: null,
	alpha: 1,


	init: function( x, y, settings ) {
		this.parent( x, y, settings );
	},

	update: function() {
		this.parent();

		--this.timer;

		if ( this.timer < 30 ) {
			this.font.alpha = 1 * (this.timer/30);
		}

		if ( this.timer <= 0 ) {
			this.kill();
		}
	},
	
	draw: function() {
		this.parent();
		this.font.draw( this.message, this.pos.x, this.pos.y );
	}
});

});

	showMessage: function(message) {
		var offsetx = (384-(message.length*6))/2;

		if ( offsetx < 42 )
			offsetx = 42;

		var rows = Math.floor(message.length/384);

		if ( rows < 1 ) 
			rows = 1;

		var offsety = (384/2)-(rows*24);
		var newMessage = ig.game.spawnEntity(EntityMessage, offsetx+64, offsety, { message: message, alpha: 1, timer: 300 });
		newMessage.previousMessage = this.currentMessage;

		for ( var last = newMessage.previousMessage; last != null; last = last.previousMessage ) {
			offsety -= 24;
			if ( offsety < 0 )
				break;
			last.pos.y = offsety;
		}

		this.currentMessage = newMessage;
	}

Any ideas why this is?

1 decade ago by zedd45

I believe this is because you did not use the "new" keyword here:

var newMessage = new ig.game.spawnEntity(EntityMessage, offsetx+64, offsety, { message: message, alpha: 1, timer: 300 });

probably just oversight, but for more info check out:
http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript
Page 1 of 1
« first « previous next › last »