# Condense this code?

**URL:** https://forum.kirupa.com/t/condense-this-code/186212
**Category:** flash
**Created:** [April 19, 2006, 7:23pm UTC](https://forum.kirupa.com/t/condense-this-code/186212 "2006-04-19T19:23:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jillymo](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jillymo](https://forum.kirupa.com/u/jillymo)
#### Post date: [April 19, 2006, 7:23pm UTC](https://forum.kirupa.com/t/condense-this-code/186212/1 "2006-04-19T19:23:05Z")

</div>

How would I simplify this code so I don’t keep repeating the onEnterFrame functions for each movieclip? (or can I?)

```auto
function simpleFade() {
	clearInterval(simpleFadeInterval);
	ground.onEnterFrame = function() {
		this._alpha -= 25;
		if (this._alpha<0) {
			this._alpha = 0;
			delete this.onEnterFrame;
}
};
   alone.onEnterFrame = function() {
		this._alpha -= 25;
		if (this._alpha<0) {
			this._alpha = 0;
			delete this.onEnterFrame;
}
};
   top.onEnterFrame = function() {
		this._alpha -= 25;
		if (this._alpha<0) {
			this._alpha = 0;
			delete this.onEnterFrame;
}
};
   crack.onEnterFrame = function() {
		this._alpha -= 25;
		if (this._alpha<0) {
			this._alpha = 0;
			delete this.onEnterFrame;
}
};
   roots.onEnterFrame = function() {
		this._alpha -= 25;
		if (this._alpha<0) {
			this._alpha = 0;
			delete this.onEnterFrame;
}
};
}
simpleFadeInterval = setInterval(simpleFade, 2000);

```

---

<div class="post-metadata">

### Author: ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)
#### Post date: [April 19, 2006, 7:47pm UTC](https://forum.kirupa.com/t/condense-this-code/186212/2 "2006-04-19T19:47:38Z")

</div>

can’t you just create a prototype function on the main timeline and call it when you need it?

---

<div class="post-metadata">

### Author: ![jillymo](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jillymo](https://forum.kirupa.com/u/jillymo)
#### Post date: [April 19, 2006, 8:47pm UTC](https://forum.kirupa.com/t/condense-this-code/186212/3 "2006-04-19T20:47:03Z")

</div>

Yes, probably but for me to take the time to figure that out is not really worth it, although I do appreciate the suggestion.

I was really just looking for suggestions that I could learn from, so maybe when I have some spare time I will look into that- thank you =)

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [April 19, 2006, 9:19pm UTC](https://forum.kirupa.com/t/condense-this-code/186212/4 "2006-04-19T21:19:53Z")

</div>

```auto
var stuff:Array = new Array(ground, alone, top, crack, roots);
simpleFadeInterval = setInterval(simpleFade, 2000);
function simpleFade() {
 clearInterval(simpleFadeInterval);
 for (var i:Number = 0; i < stuff.length; i++) {
  stuff*.onEnterFrame = function():Void {
   this._alpha -= 25;
   if (this._alpha <= 0) {
    this._alpha = 0;
    delete this.onEnterFrame;
   }
  };
 }
}

```

Just add all of the movie clips you want to fade to the _stuff_ array 🙂

---

<div class="post-metadata">

### Author: ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)
#### Post date: [April 19, 2006, 9:21pm UTC](https://forum.kirupa.com/t/condense-this-code/186212/5 "2006-04-19T21:21:59Z")

</div>

Assuming the code is on the same timeline that addresses the movieclips, otherwise you’ll have to put the path into the array as well right?

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [April 19, 2006, 9:23pm UTC](https://forum.kirupa.com/t/condense-this-code/186212/6 "2006-04-19T21:23:55Z")

</div>

> [@lunatic](#):
>
> Assuming the code is on the same timeline that addresses the movieclips, otherwise you’ll have to put the path into the array as well right?

Yep 🙂

---

<div class="post-metadata">

### Author: ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)
#### Post date: [April 19, 2006, 9:26pm UTC](https://forum.kirupa.com/t/condense-this-code/186212/7 "2006-04-19T21:26:02Z")

</div>

Whew! Wasn’t sure I had that right. Thanks. :pleased:

---

<div class="post-metadata">

### Author: ![jillymo](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jillymo](https://forum.kirupa.com/u/jillymo)
#### Post date: [April 20, 2006, 2:08pm UTC](https://forum.kirupa.com/t/condense-this-code/186212/8 "2006-04-20T14:08:13Z")

</div>

Thanks to both of you, I appreciate the input. I probably should have though of using an array on my own…:h: But that’s easy to see now that you have posted.

=)
