A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Need some help cleaning this code

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    25

    Need some help cleaning this code

    Hey all, I can't say that I'm new to AS3, I've been working with it alot, and doing loads of stuff with it, and it's great so far, but one thing I have always had trouble with, is the amount of code I use. I'm pretty sure there is a cleaner, simpler way of doing things, but I guess I'll have to learn a bit at a time.

    So I'll start here.

    This is part of the code I'm using for a little experiment that I've put together. It works fine, just needs cleaning.

    There is a single movieclip on the stage, and to start, it is duplicated to 4 childs. These childs have 4 seperate rotations (0,90,180,270) and then they are duplicated lots of times on those settings, using a timer.

    I was sure that I saw a simpler way of doing this somewhere, but I really can't remember where I saw it, and I haven't the foggiest what I need to look for, simply because I don't know what it's called.

    I'll also add the fla file, just in case.
    Any help would be awesome! Thanks guys!
    ATT: Light_Rotation.fla

    Code:
    function createTrailBall(e:Event):void {
    	
    	// Create Child1 and add info
    	var trailBall1:Orb=new Orb();
    	trailBall1.x = trailPosition;
    	trailBall1.y = trailPosition;
    	trailBall1.rotation = 0;
    	
    	// Create Child2 and add info
    	var trailBall2:Orb=new Orb();
    	trailBall2.x = trailPosition;
    	trailBall2.y = trailPosition;
    	trailBall2.rotation = 90;
    	
    	// Create Child3 and add info
    	var trailBall3:Orb=new Orb();
    	trailBall3.x = trailPosition;
    	trailBall3.y = trailPosition;
    	trailBall3.rotation = 180;
    	
    	// Create Child4 and add info
    	var trailBall4:Orb=new Orb();
    	trailBall4.x = trailPosition;
    	trailBall4.y = trailPosition;
    	trailBall4.rotation = 270;
    	[COLOR="DimGray"]
    	//Start the next function for rotation and alpha changes
    	trailBall1.addEventListener(Event.ENTER_FRAME,animateTrailBall);
    	trailBall2.addEventListener(Event.ENTER_FRAME,animateTrailBall);
    	trailBall3.addEventListener(Event.ENTER_FRAME,animateTrailBall);
    	trailBall4.addEventListener(Event.ENTER_FRAME,animateTrailBall);
    	
    	//Add Childs to stage
    	addChildAt(trailBall1,0);
    	addChildAt(trailBall2,0);
    	addChildAt(trailBall3,0);
    	addChildAt(trailBall4,0);
    
    }
    // Function for changeing Childs on stage
    function animateTrailBall(e:Event):void {
    	e.target.rotation += rotationSpeed;
    	e.target.alpha -= 0.01;
    	if (e.target.alpha <= 0) {
    		e.target.removeEventListener(Event.ENTER_FRAME,animateTrailBall);
    		removeChild((MovieClip)(e.target));
    	}
    }
    Attached Files Attached Files
    Last edited by raven3962; 08-31-2008 at 05:40 AM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    function createTrailBall(e:Event):void {
        var trailBall:Orb;
        for(var i:int=0; i < 4; i++){
    	
    	// Create Child and add info
    	trailBall:Orb=new Orb();
    	trailBall.x = trailPosition;
    	trailBall.y = trailPosition;
    	trailBall.rotation = i*90;
    	//Start the next function for rotation and alpha changes
    	trailBall.addEventListener(Event.ENTER_FRAME,animateTrailBall);
    	
    	//Add Childs to stage
    	addChildAt(trailBall,0);
        }
    }
    
    // Function for changing children on stage
    function animateTrailBall(e:Event):void {
    	e.target.rotation += rotationSpeed;
    	e.target.alpha -= 0.01;
    	if (e.target.alpha <= 0) {
    		e.target.removeEventListener(Event.ENTER_FRAME,animateTrailBall);
    		removeChild((MovieClip)(e.target));
    	}
    }

  3. #3
    Junior Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    25
    That's awesome man, thanks very much!

    Can anyone give me a small "guide" as to what that 'for' function is and how it works? Or link me to somewhere?

    That would be awesome,
    Thanks again!

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    http://livedocs.adobe.com/flash/9.0/...ments.html#for

    for loops are a basic looping mechanism in almost all programming languages (including as1 and as2). It simply initializes with one statement, loops over some block of code while a second statement is true, and changes something between loops with a third statement. Usually it's used as above to go from one number to another hitting all of them in between.

    Honestly, that's a weird question from someone who
    can't say that I'm new to AS3, I've been working with it alot, and doing loads of stuff with it

  5. #5
    Junior Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    25
    Heh, yeah I know, but I've always done things the long way round!
    Thanks for the info man, relaly appreciate it =D

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