Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simplifying AS3 code

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
Hello

I am breaking into the flash coding realm and am finding some difficulty with writing repetitive code....I would like to find a way to streamline this code so it is not so repetitive.

Code:
function update(){
	if (!needsUpdate) return;
	needsUpdate = false;
	var tHeight, tWidth, tAlpha;
	if (big) {
		tHeight = bigHeight;
		tWidth = bigWidth;
		tAlpha = bigAlpha;
	} else {
		tHeight = smallHeight;
		tWidth = smallWidth;
		tAlpha = smallAlpha;
	}
	if (tHeight != this.height){
		if (Math.abs(tHeight - this.height) < 5){
			this.height = tHeight;
		} else {
			this.height += (tHeight - this.height) * .1;
		}
		needsUpdate = true;
	}
	
	if (tWidth != this.width){
		if (Math.abs(tWidth - this.width) < 5){
			this.width = tWidth;
		} else {
			this.width += (tWidth - this.width) * .1;
		}
		needsUpdate = true;
	}
	
	if (tAlpha != this.alpha){
		if (Math.abs(tAlpha - this.alpha) < 0.5){
			this.alpha = tAlpha;
		} else {
			this.alpha += (tAlpha - this.alpha) * .1;
		}
		needsUpdate = true;
	}
	if (needsUpdate) {
		setTimeout(update,10);
	}
}

I need to expand it to account for x and y positioning too, and so I'll need to add two more sections that are exactly the same as the above chunks, with the exception of the data that is changing.

To me, this repetitive code screams 'loop over an array' or 'object orient me' but I cannot seem to figure out how I can accomplish it. If I could pass primitives by reference, I could do it easy enough...but AS3 only passes primitives by value.

Any ideas?

Robert Carpenter
Remember....eternity is much longer than this ~80 years we will spend roaming this earth.
ô¿ô
 
Ooooh yea. Some info on what the script is trying to accomplish? Sure....

I am designing a menu to be potentially used on a website. The word 'Menu' is a trigger, and when the mouse hovers over it, an orbital menu flys out around it. Because each menu item will be doing the same animation, only with different destinations, I am trying to module-ize the code to cut down on typing and download time, etc.

Robert Carpenter
Remember....eternity is much longer than this ~80 years we will spend roaming this earth.
ô¿ô
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top