The var myStars declaration in Starfield.init is useless, you can safely get rid of that.
Huh? But the global declaration...
var myStars = new Array();
...only makes myStars an Array, it doesn't fill it with anything. The initialization's...
var myStars = this.number;
...makes the size of myStars whatever size you set it to in Starfield.init, so you can change the number of stars on-the-fly.
Weird, though, you're right -- when I comment it out, nothing changes, the code runs fine. I don't understand -- how does myStars know to be 32 stars in length (thanks to the "MyStarfield.init(32,2,4,8);" call in game()) without that declaration?
Second, variable scope once again plays a role in your code; you can't use myStars[ i ] after the for-loop since i will point to an "out-of-bounds" index.
As for the time comparison, I don't know what's going on there but it kind of looks like you're trying to give the star some velocity. It would be a good idea to look up some game physics tutorials for cleaner ways to do this; Fix Your Timestep and one of CodeIncomplete's JS game dev tutorials are really good starting points, though keep in mind the CodeIncomplete tutorials mainly focus on Web JS.
Ah, nice resources, thanks. Yeah, this is kind of a mess of code. I'm sort of building up towards what I want. Since I ran into problems merely displaying the stars, I got halted. The plan is obviously to have them move, eventually.
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() {
}
Yeah, I knew it didn't have a syntactic purpose, I meant I thought he was using it as a convention to denote iterated thingies. When basing my code on others code, I typically just use their naming conventions so that I can have an easier time going back and forth from reading their code to modifying mine. I'll rename everything at the end.
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();
}
Ahh! Right! I think I knew that at one point, it looks very familiar. Pretty sure I did that on my TextTyper function to provide a default size to the text box if it's not specified. Thanks.
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.
The array is filled elsewhere, with "_Star(x,y)" objects. What that bit of code was intended to do (and realistically, I didn't expect it to be this easy, but I figured I'd try anyway) was, since each _Star in the array had an x and y value, advance the y value of each, thereby creating a star scrolling effect.
But I guess you can't use dot operators on individual elements of an Array the way I tried to there.
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.
Er, "pop" here is short for "populate." Populate the Starfield. I didn't know that ".pop" was a thing.
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));
}
}
Ah okay, see, I was segregating basically every single function of Starfield into its own prototype because I wasn't 100% sure what could and couldn't be safely combined. I figured, get everything working independently first, then cram it all together.
Also that "this.myStars = new Array(this.number)" is awesome! So much neater than declaring myStars out in the open then manipulating it in .init.
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();
}
}
It may indeed, and thanks for cleaning all that up for me. Somehow, though, I suspect that "this.myStarsI.y++;" still won't have the effect of moving the stars I was hoping for, heh. Which is okay, 'cause I'm still trying things, and since I was able to get basic physics working in my platformer, I know I'll be able to get them working here once I get back into the swing of things.
(Can't do "i" in ][ these brackets outside of a code tag or it gets interpreted as itallicize :/)
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".
Don't be! I learn best when I try, fail utterly, then have someone explain to me where I went wrong. You've been a huge help
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.
I was doing some passable physics when I was working on my simple platformer last year, but I've been away for so long I've forgotten most of it and will have to relearn the hard way.
Thanks for all the help
(BTW, still reeeally weirded out by the fact that LoadImage("star.PNG") and LoadImage("star.png") are considered distinct. I've never, ever known file extensions to be case-sensitive, that seems so weird to me.)