Hey Mooch, you're getting closer, here's some tips on programming cleaner:
The underscore means nothing, you don't have to use it. It's a personal convention usually indicating a privately scoped function (something very complex that we don't need to get into right now). It's not a syntax thing, it's just a thing.
function _Star() {
}
// rename it to (make sure you rename it everywhere else too!):
function Star() {
}
Next, you can shorten this area:
Starfield.prototype.init = function(setNum,setSpd1,setSpd2,setSpd3){
this.number = setNum;
this.speed1 = setSpd1;
this.speed2 = setSpd2;
this.speed3 = setSpd3;
if(!setNum){this.number = 108;}
if(!setSpd1){this.speed1 = 2;}
if(!setSpd2){this.speed2 = 4;}
if(!setSpd3){this.speed3 = 8;} // default values for safety's sake
this.time = GetTime();
var myStars = this.number; // Like NEO said, this does nothing. Can you guess as to why?
}
Like so:
Starfield.prototype.init = function(setNum,setSpd1,setSpd2,setSpd3){
this.number = setNum || 108;
this.speed1 = setSpd1 || 2;
this.speed2 = setSpd2 || 4;
this.speed3 = setSpd3 || 8;
this.time = GetTime();
}
A problem with your iteration, like neo said is that myStars is meaningless here:
for(var i=0; i<this.number; i++){
if (this.time < GetTime()) {
myStars[i].y++;
this.time = GetTime();
}
}
myStars here is treated as an array. But you have to fill that array with values. An array in JS can hold 1 or many objects.
You fill this array in Starfield.prototype.pop(), which makes no sense to me since .pop is a function usually used for removing an item from the bottom of an array.
That stuff can go into Starfield.prototype.init, like so:
Starfield.prototype.init = function(setNum,setSpd1,setSpd2,setSpd3){
this.number = setNum || 108;
this.speed1 = setSpd1 || 2;
this.speed2 = setSpd2 || 4;
this.speed3 = setSpd3 || 8;
this.time = GetTime();
this.myStars = new Array(this.number); // this is the correct way to 'save' myStars
// creates this.number stars at random x/y positions
for(var i=0; i<this.number; i++){
this.myStars[i] = new Star(Math.floor(Math.random()*MyGame.screenWidth),
Math.floor(Math.random()*MyGame.screenHeight));
}
}
Now, your exe function may look like this:
Starfield.prototype.exe = function(){
this.time = GetTime();
for(var i=0; i<this.number; i++) {
StarImg.blit(this.myStars[i].x,this.myStars[i].y);
}
if (this.time < GetTime()) {
this.myStars[i].y++;
this.time = GetTime();
}
}
I'm very sorry to have to show you how to code this, but I implore you to compare your stuff with mine, as you can see, there are many scoping issues you still made. I can tell, you never learned from my lesson regarding the property "myStars".
Also do what NEO said and look up basic game physics and the like. Fair warning, you will need to know math. A good programmer can usually visualize the math in their heads. But if you aren't good at math, that's fine too. Use JavaScript as a sandbox to test out math and get an appreciation for it. It's the best I can say. Programming made me a better mathematician than Math courses in school.