as3learner
as3learner

Reputation: 29

AS3 - Call function with forEach

Flash throw this error in the Output panel: ReferenceError: Error #1069: Property alpha not found on String and there is no default value.

The problematic code:

var mcs:Array = new Array();
mcs.push({name:"mc_1"});
mcs.push({name:"mc_2"});
mcs.push({name:"mc_3"});
mcs.push({name:"mc_4"});

mcs.forEach(fade);

function fade(element:*, index:int, arr:Array):void 
{
    fl_FadeOut(element.name);
}

function fl_FadeOut(element:Object)
{
    element.alpha -= 0.05;
    if(element.alpha <= 0)
    {
        element.removeEventListener(Event.ENTER_FRAME, fl_FadeOut);
    }
}

I just want to fade out mc_1 then fade in mc_2, hold it for a second then fade out. mc_3 fade in...etc But I stucked at this error message. Thanks for any help!

Upvotes: 1

Views: 79

Answers (1)

Marty
Marty

Reputation: 39456

You're passing element.name to fl_FadeOut, which is a string.

I suspect you want to do something along the lines of:

function fade(element:*, index:int, arr:Array):void 
{
    fl_FadeOut(getChildByName(element.name));
    //         ^^^^^^^^^^^^^^
}

Where you use getChildByName() to reference a DisplayObject by its name.

Upvotes: 2

Related Questions