-
"i" in loops
I was just wondering what do you pro's use instead of "i" in a loop if you have already used it in a previous loop? I cant seem to find a nicer letter to replace it.
Can i make the "i" only affect the function it is in so i can use it over and over again?
Which is better:
One big loop containing everything
A couple of smaller loops.
I only plan to loop threw about 30 things max at once.
-
a) You can use any letter you want.
b) Its better to have one or as few loops as possible.
c) Look into for statements those allow you to reuse variables for the counter
d) You can reset the value of i after every loop is done.
-
i think, if you define i within a function, like this: var i; (or whatever the 2.0 syntax is) i think it stays within the function.
-
If i dont get around having one iterating variable "i" and need to have 4 for example its easy to just go by the alphabet.
Code:
for(var i:Number=0; i<len1; i++){
for(var j:Number=0; j<len2; j++){
for(var k:Number=0; k<len3; k++){
for(var l:Number=0; l<len4; l++){
}
}
}
}
But if you don't need to have several different values parallel you can also use just one iterating variable and reset it.
Code:
for(var i:Number=0; i<len1; i++){
}
for(i=0; i<len1; i++){
}
But another handy way is to just use a function in most cases..
Code:
function doStufftoArray(tArray:Array):Void {
for(var i=0; i<tArray.length; i++){
// If i use the object for more then one thing i store it in a temporary var too
var tObj:Object = tArray[i];
}
}
Hopefully this helps..
greets
-
Yea i figured out that i could just define i with a var before it and it only effected that function. In figuring that out i realised that wasnt my problem, i have 2 arrays one for bullets one for enemies, for some reason when i shoot (attach a bullet to the stage) it removes an enemy.
Code:
function NewBulletArray() {
var i;
BulletArray = new Array();
for (i=0; i<BulletCount; i++) {
BulletArray[i] = 0;
}
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
function GetNextBullet() {
var i;
for (i=0; i<BulletCount; i++) {
if (BulletArray[i] == 0) {
return i;
}
}
return -1;
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
function AttachBullet() {
var i = GetNextBullet();
if (i>=0) {
BulletArray[i] = 1;
var Bullet = _root.attachMovie("bullet", "BulletArray"+i, i);
//BULLETCODE//
}
}
As you can see i make a new array called BulletArray[i] and set seach slot to 0 (empty) then i have a function for searching threw my array for the first avalible slot when found i attach a bullet and change that slot to 1(full). That works fine but i have another array set up identicly the same way for the enemies, and as i said before it removes some of the enemies when i shoot.
Any idea why?
-
Here is a for in tutorial I wrote, I hope it helps:
http://newgrounds.com/bbs/topic.php?id=298078
-
It sounds like a depth problem...Your new bullet probably has the same depth as an ennemy MC.
-
Gloo,
Razorback has a good point: your bullets are probably sharing depth with one or more enemies.
To keep this from happening, you can create an empty MovieClip called "renderBullets" and another called "renderEnemies." Attach all of your bullets to the "renderBullets" MovieClip, and all of your enemies to the "renderEnemies" MovieClip. That way, enemies and bullets will never have depth issues. You just need to nest all of each into their own seperate container clips.
+Q__
-
It was depth problems, i just made shore they all had there own depths but i didnt understand how to put all of them into MC's. I am guessing that will help me with my next problem.
How can i preform a hittest with 2 objects in an array? If it has somthing to do with putting them into MC's can you please tell me how this is done?