# Can I condense this?

**URL:** https://forum.kirupa.com/t/can-i-condense-this/262419
**Category:** flash
**Created:** [June 5, 2008, 7:07pm UTC](https://forum.kirupa.com/t/can-i-condense-this/262419 "2008-06-05T19:07:35Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![rondog](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rondog/32/2478_2.png) [@rondog](https://forum.kirupa.com/u/rondog)
#### Post date: [June 5, 2008, 7:07pm UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/1 "2008-06-05T19:07:35Z")

</div>

for the rollOver rollOut functions, in AS2 I could just run the function in the loop like this:  
AS2:

```auto

    var names:Array = new Array("Video Introduction", "2008-2009 Catalog");
    for (var i = 0; i < names.length; i++) {
        this["btn" + i].txt.text = names*;
        this["btn" + i].btnbg.onRollOver = function() {
            this.alphaTo(40,0.3,"easeOutExpo");
            this._parent.arrow.slideTo(238,null,0.5,"easeOutBounce");
        };
        this["btn" + i].btnbg.onRollOut = function() {
            this.alphaTo(80,0.3,"easeOutExpo");
            this._parent.arrow.slideTo(288,null,0.5,"easeOutBounce");
        };
    }

```

but for AS3, the only way I know how to do it is to create a function outside of the loop like this:

```auto

function init():void
{
    var names:Array = new Array("Video Introduction", "2008-2009 Catalog");
    for (var i:uint = 0; i<2; i++)
    {
        this["btn" + i].txt.text = names*;
        this["btn" + i].txt.mouseEnabled = false;
        this["btn" + i].btnbg.addEventListener(MouseEvent.MOUSE_OVER, btnOver);
        this["btn" + i].btnbg.addEventListener(MouseEvent.MOUSE_OUT, btnOut);
        this["btn" + i].btnbg.buttonMode = true;
        this["btn" + i].btnbg.useHandCursor = true;
    }
}
init();

function btnOver(e:MouseEvent):void
{
    TweenLite.to(e.target,0.3,{alpha:0.4,ease:Exponential.easeOut});
    TweenLite.to(e.target.parent.arrow,0.5,{x:238,ease:Bounce.easeOut});
}

function btnOut(e:MouseEvent):void
{
    TweenLite.to(e.target,0.3,{alpha:0.8,ease:Exponential.easeOut});
    TweenLite.to(e.target.parent.arrow,0.5,{x:288,ease:Bounce.easeOut});
}

```

so any way to condense the as3 version or is that pretty much it?

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [June 5, 2008, 7:22pm UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/2 "2008-06-05T19:22:40Z")

</div>

That’s pretty much the idea.

---

<div class="post-metadata">

### Author: ![Dom\_1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Dom\_1](https://forum.kirupa.com/u/Dom_1)
#### Post date: [June 5, 2008, 7:23pm UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/3 "2008-06-05T19:23:45Z")

</div>

```auto

function init():void
{
    var names:Array = ["Video Introduction", "2008-2009 Catalog"];
    for (var i:uint = 0; i<2; i++)
    {
        this["btn" + i].txt.text = names*;
        this["btn" + i].txt.mouseEnabled = false;
        this["btn" + i].btnbg.addEventListener(MouseEvent.MOUSE_OVER, setState);
        this["btn" + i].btnbg.addEventListener(MouseEvent.MOUSE_OUT, setState);
        this["btn" + i].btnbg.buttonMode = true;
        this["btn" + i].btnbg.useHandCursor = true;
    }
}
init();

function setState (e:MouseEvent):void
{
	if (e.type == MouseEvent.MOUSE_OVER)
	{
		TweenLite.to(e.target,0.3,{alpha:0.4,ease:Exponential.easeOut});
		TweenLite.to(e.target.parent.arrow,0.5,{x:238,ease:Bounce.easeOut});
	} else if (e.type == MouseEvent.MOUSE_OUT) {
		TweenLite.to(e.target,0.3,{alpha:0.8,ease:Exponential.easeOut});
		TweenLite.to(e.target.parent.arrow,0.5,{x:288,ease:Bounce.easeOut});		
	}
}

```

note: using the [] syntax when creating an array is (much) faster than the literal syntax 🙂

---

<div class="post-metadata">

### Author: ![rondog](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rondog/32/2478_2.png) [@rondog](https://forum.kirupa.com/u/rondog)
#### Post date: [June 5, 2008, 7:26pm UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/4 "2008-06-05T19:26:57Z")

</div>

I kinda figured anogar that was pretty much it hehe. I do like the idea of just using one function though like you did dom. Cleaned up nicely. Thanks!

---

<div class="post-metadata">

### Author: ![Dom\_1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Dom\_1](https://forum.kirupa.com/u/Dom_1)
#### Post date: [June 5, 2008, 7:28pm UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/5 "2008-06-05T19:28:27Z")

</div>

your wlecom

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [June 5, 2008, 9:08pm UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/6 "2008-06-05T21:08:37Z")

</div>

It’s worth nothing that despite having condensed it into a single function, you’re making it check the conditional statement every time you click on something now, which is minor, but it’s an unnecessary performance barrier just to mash those two functions together. You’re not getting anything, functionally, out of it in return.

I’m a big fan of organization and clean coding, but not at the expense of an illogical performance hit like that with no return. (I know it’s not a big hit, it’s just the principal of it.)

---

<div class="post-metadata">

### Author: ![Dom\_1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Dom\_1](https://forum.kirupa.com/u/Dom_1)
#### Post date: [June 5, 2008, 9:30pm UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/7 "2008-06-05T21:30:03Z")

</div>

spot on Anogar

---

<div class="post-metadata">

### Author: ![Dom\_1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@Dom\_1](https://forum.kirupa.com/u/Dom_1)
#### Post date: [June 6, 2008, 6:34am UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/8 "2008-06-06T06:34:57Z")

</div>

I use anonymous functions like that sometimes too, but I never thought about the whole closure thing, thanks for pointing that out LordMoyne

---

<div class="post-metadata">

### Author: ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)
#### Post date: [June 6, 2008, 6:53am UTC](https://forum.kirupa.com/t/can-i-condense-this/262419/9 "2008-06-06T06:53:24Z")

</div>

> [@LordMoyne](#):
>
> Note that there is an added benefit to this in that you’ve created a closure with those functions.

Technically, functions declared (using the function statement) anywhere in AS are closures, so the added benefit to the code that you posted is that the scope of the alternatively-placed function includes additional, potentially useful variables, not that closures hadn’t been created in the old code.
