1 decade ago
by paulh
Is the following class defined properly.
how do i call the class to trigger the init?
how do i call runme?
In both cases from outside the class....
(pretty sure this is the last thing i have to work out to get the BASICS)
ig.module(
'game.words'
)
.requires(
'impact.game'
)
.defines(function(){
Words = ig.Class.extend({
init: function(){
console.log("init");
},
runme: function(){
console.log("i ran");
}
});
});
Don't forget the parentheses.
var word = new Words();
1 decade ago
by paulh
thanks alexandre, it worked without the (); ... wudl you mind educating me on why?
also
still cant get runme();
:-(
1 decade ago
by Arantor
Actually in JS the () are not strictly required, it is generally good practice.
.init() is not normally called just defining the class. You have to call word.init(); after you've declared word as an instance.
Impact will call .init() in a lot of cases for you but runme() won't just spontaneously call by itself, you'll have to use word.runme(); or similar.
1 decade ago
by paulh
yea ive been trying var word = new Words.runme();
but get
Uncaught TypeError: undefined is not a function
1 decade ago
by paulh
so this works: word = new Words();
produces : "init"
so howcome that works, rather than:
.init() is not normally called just defining the class. You have to call word.init(); after you've declared word as an instance.
sorry for such basic questions!
Paul, here's an example:
Words = ig.Class.extend({
init: function()
{
console.log('words instance init');
},
runme: function()
{
console.log('words instance runme');
}
});
Clients would then use it like this:
var w = new Words();
w.runme();
with matching output in the console:
words instance init
words instance runme
1 decade ago
by Heiko
Or you can call
runme
from inside
init
, if it should ALWAYS be called on init.
Another cool coding practice is chaining (google object/function chaining).
Words = ig.Class.extend({
init: function()
{
console.log('words instance init');
this.runme();
},
reset: function()
{
console.log('words instance reset');
return this; // allows for chaining
},
runme: function()
{
console.log('words instance runme');
}
});
// this will init, and execute run immediately on init
var w = new Words();
// reset and run again
w.reset().runme();
// reset and run again (alternative formatting)
w.reset()
.runme();
@Heiko
Very nice. I'm relatively fresh at js and keep forgetting to do that. Thanks for reminder.
1 decade ago
by paulh
yes, thank to you all, your awesome!!!!
reduced a file by 1.2MB and saved 60ms!!!!
last question is about memory.
Lets say my class file words is 1MB, when is that stored/loaded in memory, on load of ig.game or when i call var w = new Words();?
I think its loaded on game load, but can someone confirm and if so, im assuming that entities are different, in that they are loaded into memory when created and killed on this.kill?
Page 1 of 1
« first
« previous
next ›
last »