1 decade ago by quidmonkey
				Okay, this is a bit of a doozy. My thanks ahead of time for looking into this.
I'm trying to extend the Font class to pop-up various notifications to the player, which will then fade after a few seconds. Here's what I got so far:
The chain-notification.js file is placed in the /game/impact/ directory. I want to be able to instantiate these notifications whenever I want, so I've modded my main.js accordingly:
When I try to call ig.main.spawnNote() from any Entity class, I get this:
		I'm trying to extend the Font class to pop-up various notifications to the player, which will then fade after a few seconds. Here's what I got so far:
ig.module(
	'impact.chain-notification'
)
.requires(
	'impact.font'
)
.defines(function(){
ChainNotification = ig.Font.extend({
	font: null,
	text: '',
	pos: { x: null, y: null },
	_kill: null,
	vel: { x: 0, y: -10 },
        
	lifetime: 5,
	fadetime: 1,
        
        init: function( text, x, y ) {
            
		this.font = new ig.Font( 'media/04b03.font.png' );
		this.text = text;
		this._kill = false;
                
		this.pos.x = x;
		this.pos.y = y;
		
		this.idleTimer = new ig.Timer();
	},
	
	
	update: function() {
		
		//if greater than lifetime, kill note
		if( this.idleTimer.delta() > this.lifetime ) {
			this._kill = true;
			return;
		}
                
		//slowly dissipate
		this.currentAnim.alpha = this.idleTimer.delta().map(
			this.lifetime - this.fadetime, this.lifetime, 1, 0 );
		
		//update position
		this.pos.x += this.vel.x;
		this.pos.y += this.vel.y;
		
	},
	
	draw: function() {
		this.font.draw( this.text, this.pos.x, this.pos.y, ig.Font.ALIGN.LEFT );
	}
    
});
});
The chain-notification.js file is placed in the /game/impact/ directory. I want to be able to instantiate these notifications whenever I want, so I've modded my main.js accordingly:
ig.module( 
	'game.main' 
)
.requires(
	'impact.game',
	'impact.font',
	//other stuff
)
.defines(function(){
MyGame = ig.Game.extend({
	
	notes: [],	//my notifications array
	//code
	update: function() {
		// Update all entities and backgroundMaps
		this.parent();
		
		if( this.notes.length ) {
			for( i = this.notes.length; i--; i ) {
				this.notes[i].update();
				
				//if note is dead, erase it
				if( this.notes[i]._kill ) {
					this.notes.splice(i, 1);
				}
			}
		}
	},
	draw: function() {
		//other code
		//draw notes
		if( this.notes.length ){
			for(var note in this.notes) {
				this.notes[note].draw();
			}
		}
		
	}
};
ig.main( '#canvas', MyGame, 60, 528, 576, 1 );
//create note
ig.main.spawnNote = function( text, x, y) {
	var note = new ChainNotification( text, x, y );
	MyGame.notes.push( note );
};
});
When I try to call ig.main.spawnNote() from any Entity class, I get this:
Uncaught TypeError: Object [object Object] has no method 'spawnNote'Any ideas on what's broken?
