Search code examples
flashactionscript-3actionscriptmxml

Passing values through functions in mxml?


I'm fairly new to MXML and Flash Builder with a fair bit of experience with AS3 in Flash Professional. My question is that I currently have a basic application where we have multiple buttons, which have functions activated by "click" event listeners on those buttons. The issue is that each button does more or less the same function; just the data that is loaded for each button is different. How do I make it so I'm able to recycle the same function? In Javascript or AS3 something along these lines would be done...

<s:Button id="btn_1" x="378" y="601" label="Button 1" click="photoSwap(event,"image1.jpg")"/>
<s:Button id="btn_2" x="350" y="601" label="Button 2" click="photoSwap(event,"image2.jpg")"/>

protected function photoSwap(event:MouseEvent):void
{
    // TODO Auto-generated method stub
    var selectedImage = evt.target;
    Tweener.addTween(img, {alpha:0, time:1});
    img.source="images/" + selectedImage;
    Tweener.addTween(img, {alpha:1, time:2, delay:1});
}

But that's obviously not how it works. I've tried trouble shooting the issue but haven't been able to find anything. It works when I have each button having its own function, but that seems really silly and shouldn't need to be done.


Solution

  • A very common Flash Builder rookie error.

    MXML tags have attributes which are specified in double-quotes ("). So, it is natural to disallow " in the attribute value. All you need to do is specify strings in MXML attributes using the single-quote (')

    <s:Button id="btn_1" x="378" y="601" label="Button 1" click="photoSwap(event,'image1.jpg')"/>
    <s:Button id="btn_2" x="350" y="601" label="Button 2" click="photoSwap(event,'image2.jpg')"/>
    
    protected function photoSwap(event:MouseEvent, selectedImage:String):void
    {
        Tweener.addTween(img, {alpha:0, time:1});
        img.source="images/" + selectedImage;
        Tweener.addTween(img, {alpha:1, time:2, delay:1});
    }