-
-
Save quidmonkey/1065058 to your computer and use it in GitHub Desktop.
/* | |
* Impact Plugin | |
* NotificationManager | |
* Written by Abraham Walters | |
* July 2011 | |
* Jxyzzy Dev Company | |
* jxyzzy.com | |
* | |
* This plugin extends the Font class and allows you to pop-up a | |
* text Notification (spawnNote()), move it (this.pos) and have | |
* it fade after a specified amount of time (this.lifetime & | |
* this.fadetime). The plugin provides a NotificationManager to | |
* do all your dirty work and track each Notification for updating | |
* and drawing. | |
* | |
* To use the plugin you will need to create an instance of the | |
* NotificationManager in your ig.game instance (MyGame) like so: | |
* | |
* myNoteManager: new NotificationManager(), | |
* | |
* You will then need to add myNoteManager.update() to ig.game.update() | |
* and myNoteManager.draw() to ig.game.draw(). Make sure you add | |
* myNoteManager.draw() after the this.parent() draw, otherwise your | |
* Notifications will be drawn over. From there you can spawn a | |
* Notification within any Entity using the following syntax: | |
* | |
* ig.game.myNoteManager.spawnNote('media/font.png', 'string', x, y, settings); | |
* | |
* Enjoy! | |
* | |
*/ | |
ig.module( | |
'plugins.notification-manager' | |
) | |
.requires( | |
'impact.impact' | |
) | |
.defines(function(){ | |
ig.NotificationManager = ig.Class.extend({ | |
notes: [], | |
init: function() {}, | |
draw: function() { | |
for(var i = 0; i < this.notes.length; i++) { | |
this.notes[i].draw(); | |
} | |
}, | |
spawnNote: function( font, text, x, y, settings) { | |
var note = new Notification( font, text, x, y, settings ); | |
this.notes.push( note ); | |
}, | |
update: function() { | |
for( var 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); | |
} | |
} | |
} | |
}); | |
Notification = ig.Class.extend({ | |
font: null, //font | |
text: '', //string to draw | |
pos: { x: null, y: null }, //position | |
_kill: null, //state | |
vel: { x: 0, y: -20 }, //velocity - set to 0 if font doesn't move | |
alpha: 1, //alpha, 0 = translucent, 1 = opaque | |
lifetime: 1.2, //how long notification should last, set to zero to disable fade | |
fadetime: 0.4, //how long until note fades | |
init: function( font, text, x, y, settings ) { | |
this.font = font; | |
this.text = text; | |
this._kill = false; | |
this.pos.x = x - ig.game.screen.x; | |
this.pos.y = y - ig.game.screen.y; | |
ig.merge( this, settings ); | |
this.idleTimer = new ig.Timer(); | |
}, | |
update: function() { | |
//update position | |
this.pos.x += (this.vel.x * ig.system.tick); | |
this.pos.y += (this.vel.y * ig.system.tick); | |
//if lifetime = 0, skip fade and kill | |
if( !this.lifetime ) { | |
return; | |
} | |
//if greater than lifetime, kill note | |
if( this.idleTimer.delta() > this.lifetime ) { | |
this._kill = true; | |
return; | |
} | |
//do fade - slowly dissipate | |
this.alpha = this.idleTimer.delta().map( | |
this.lifetime - this.fadetime, this.lifetime, 1, 0 ); | |
}, | |
draw: function() { | |
//set system alpha for fade effect | |
if( this.alpha != 1) { | |
ig.system.context.globalAlpha = this.alpha; | |
} | |
//draw font | |
this.font.draw( this.text, | |
ig.system.getDrawPos(this.pos.x), ig.system.getDrawPos(this.pos.y), | |
ig.Font.ALIGN.LEFT ); | |
//reset system alpha so fade effect doesn't get applied | |
//to other objects being drawn | |
if( this.alpha != 1) { | |
ig.system.context.globalAlpha = 1; | |
} | |
} | |
}); | |
}); |
Yeah that's whats crazy, I've got those lines all in place.. and even tried swapping out the ig.game.font with a new font definition just as it is in the example, but still get the same issue. When i change the font name deliberately to be a font I dont have (ie: pullingyourhairout.png) it actually gives me a specific warning that the specific file cant be found, so i know it's not that..
my requires looks like this:
.requires(
'impact.game',
'impact.font',
'game.entities.player',
'game.entities.crate',
'game.levels.loadingstage',
'game.levels.endingstage',
'game.levels.cave',
'plugins.impact-splash-loader',
'plugins.notification-manager',
'plugins.screen-fader'
)
myGame = ig.Game.extend({
//Load Note Manager
myNoteManager: new ig.NotificationManager(),
....
update: function() {
this.myNoteManager.update();
....
}
draw: function() {
this.myNoteManager.draw();
.......
}
Then in my crate.js I have this function that calls it:
check: function( other ) {
if ( other.pickup == true && other.gotcrate == false) {
other.gotcrate = true;
this.tik.play();
ig.game.myNoteManager.spawnNote(new ig.Font( 'media/fonts/block.png' ), 'my entity message ',this.pos.x, this.pos.y, {vel: { x: 0, y: 0 }, alpha: 0.5, lifetime: 2.2, fadetime: 0.3 });
this.kill();
}
},
Let me know if you want direct access to the code, I can give that as well if it helps... I am so thankful for your help. :)
Many cheers,
Nate
Weird. You're certain that it's not something else? Try commenting out the spawnNote() and see if the error persists.
Yeah if i comment out the whole spawnNote line i dont get any errors. :(
Could I get your email address to send you a private link to my full source to look at?
Big thx to 80bit and stahlmanDesign over @ the ImpactJS forums!
We hunted down and found a bug with the Notifications not being translated to the onscreen coordinates nor taking scale into account. Plugin has been updated to reflect these changes.
Hey 80bit-
Heh, you're a funny guy. And np on the help.
Typically you get a 404 when it can't find the file. For your font - ig.game.font - did you initialize it properly, and are you sure the image file exists in the /media/ directory? Did you add 'plugins.notification-manager' to your ig.game.requires()? Did you create myNoteManager = new.igNotificationManager()? Did you create a plugins folder in the /lib/ impact directory and dump the NotificationManager.js in there? See the physics example if you need an example are where to put the plugins folder.
Best.