A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: "i" in loops

  1. #1
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874

    "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.
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  2. #2
    Razor
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    721
    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.

  3. #3
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,793
    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.

  4. #4
    n00b LeechmasterB's Avatar
    Join Date
    May 2004
    Location
    Switzerland
    Posts
    1,067
    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
    I do stuff that does stuff...

    J-Force

  5. #5
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    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?
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  6. #6
    Member
    Join Date
    Sep 2004
    Posts
    89
    Here is a for in tutorial I wrote, I hope it helps:
    http://newgrounds.com/bbs/topic.php?id=298078

  7. #7
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    It sounds like a depth problem...Your new bullet probably has the same depth as an ennemy MC.

  8. #8
    Qwaizang:syntax_entity_ Q__Hybrid's Avatar
    Join Date
    Aug 2005
    Posts
    270
    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__
    Qwai•zang \kwî-'zan\ n [origin unknown] 1 : abstract designer, esp. of complex real-time experiments, c. 21st century

  9. #9
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    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?
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center