1 decade ago
by paulh
Hi all
can someone explain to me whats happening here:
MyGame = ig.Game.extend({
init: function() {
// Initialize your game here; bind keys etc.
//console.log(x);
getnumber();
console.log(x);
console.log(y);
console.log(z);
},
ig.module('game.getnumber')
.requires(
'impact.game'
)
.defines(function() {
getnumber = function() {
alert("getnumber");
x=0;
y=1;
z=2;
}
})
So as i initialize the game, the get number function is called.
The getnumber function runs and generates x,y and z variables.
After the getnumber function has run, the process returns to main?
The variables, x,y,z are now available in main?
Some basic questions:
How long will x,y,z be stored in memory?
When or how can they be "deleted"?
Im sssuming this is MUCH better than ig.game.z, ig.game.y and ig.game.x . am i right in this thinking?
I guess there's a (better)way of making of saying something like:
//QUERY
mynumbers = getnumbers();
//RESULT
mynumbers = {x:1, y:2, z:3};
am i taking out of my arse?
1 decade ago
by paulh
And here ( i know this is all basic stuff, but id appreciate any input)
ig.module(
'game.timer'
)
.requires(
'impact.game'
)
.defines(function(){
Timer = ig.Class.extend({
secTimer:null,
init:function(){
secTimer = new ig.Timer(1);
time = secTimer.delta();
//console.log("time",time);
console.log ("delta",secTimer.delta());
},
update:function(){
time = secTimer.delta();
console.log("time",time);
console.log ("delta",secTimer.delta());
}
});
});
init: function() {
var times = new Timer();
},
This returns -1 for time.
update: function() {
console.log("time",time);
this.parent();
},
##
this returns -1, but i timer to keep running the timer, and updating the time (from timer.delta()).
Is the time still running, or doe sit just run for that first second?
If it is still running why cant i access the updated delta.
trying to get my head around some more of the basics....
1 decade ago
by Philip
your x, y and z variable are defined as a global variable. Which is, most of the time, a bad thing. Lets look at an example:
init: function( x, y, settings ) {
// x and y are now the ones from the function parameters
// there is no way to access your global x and y
// ok, I lied, you can get to them via window.x but you see where this is going?
}
The other thing is, that this is just bad programming style. What is x, y and z? Probably a position, but the position of what? The player? Then store it like this. And the best would be to store this in the game object. So:
ig.game.player = { x:0, y: 0, z: 0 }
Give everything its context. It doesn't only read better, but i leads to fewer bugs.
The same goes to the getnumbers function. Don't declare that global. Is it a database request? Make an Database class and the getnumbers function a method of that.
To the timer thing: You made that global mistake again
ig.module(
'game.timer'
)
.requires(
'impact.game'
)
.defines( function(){
Timer = ig.Class.extend({
secTimer:null,
init:function(){
// the next line acces a global secTime and not the instance variable secTime of Time
secTimer = new ig.Timer(1);
// write it like this
this.secTimer = new ig.Timer(1);
// another global varialbe. remeber, they are bad
time = secTimer.delta();
// but a var for it
var time = secTimer.delta();
// time now lives as long as it is needed. SO in this case not long, it is destroyed right after the function
}
});
});
1 decade ago
by paulh
Many thanks for the detailed reply, so i was just replicating my previous error of puttting everything as a global variable : ig.game.x, but in a different way!
ig.game.player = { x:0, y: 0, z: 0 }
this is what i need to get my head around!!
1 decade ago
by paulh
Right, i got that bit working .. :-)
Now for the timer ...
I do actually want a global timer , that i can access from anywhere, but even though you state my code example is a global timer, i still cant get to the timer variables from main?
Put it in main.js:
MyGame = ig.Game.extend({
gameTime: null,
init: function(){
//other code
this.gameTime = new ig.Timer();
}
//other code
});
It can then be accessed in any other class as
ig.game.gameTime
.
1 decade ago
by paulh
hmmm i was just looking a profile snapshot and i think i'm still making the same mistake e :-(
ig.module('game.getstuff')
.requires(
// 'impact.game'
)
.defines(function() {
getstuff = function() {
console.log(ig.game.maintimer.delta());
stuff3="1234567890";
for ( i=0; i< this.stuff.length; i++){
//blah blah
}
it seems that stuff3 is STILL A GLOBAL STRING that i can access anywhere .. just from console.log(stuff3).
Since stuff3 is a very long string, i think its being stored in memory at all times?
I thought id got it right, but looks like im far from it still :-(
How do i make stuff3 a LOCAL variable and use it in the for loop?
Thanks to everyone who helps, i started running before i could walk and now trying to rewrite everything properly :-/
Stick "var" in front of it. Any variable declared with "var" is placed in the local scope; otherwise, it gets placed in the global scope. In general, you want to use "var" wherever possible.
1 decade ago
by paulh
yea i tried var
but when i :
xc =1;
var num_stuff = this["get_stuff" + xc].length / suff_length;//
i can no longer append the var name :-(
Not append var to what? num_stuff
? get_stuff
? What's the error you're getting?
1 decade ago
by paulh
tryd to cut and paste and give you the gist (real file is huge)
var get_stuff ="12345678910112";
xc=3;
for ( i=0; i< xc; i++){
var other_Stuff = ['get_stuff' + xc].length / stuff_length;
ig.game.my_stuff =(['get_stuff' + xc].substring(n,(n+7)))
}
error = object has no method substring
1 decade ago
by paulh
ig.module('game.getword')
.requires(
)
.defines(function() {
getWord = function() {
var wordlist3 ="AAHABSACEA"
var xc=3;
var word_length = xc;
var words_in_round = 2;
for ( i=0; i< this.words_in_round; i++){
var num_words = (this['wordlist' + xc].length) / word_length;// Get the number of words in dictionary
var random = Math.floor(Math.random() * ((num_words+1) - 0 ));
n = Math.round(random / word_length) * word_length;
ig.game.random_word =(this['wordlist' + xc].substring(n,(n+word_length)))
ig.game.random_words += ig.game.random_word
}
tried with and without the "this" ...
1 decade ago
by paulh
Moved it back to arrays and then used each list as an array item:
array = new array();
array[0] = "list1";
array[2] = "list2"
works fine :-)
Page 1 of 1
« first
« previous
next ›
last »