开发者

AS3: How to make Parent Function return the selected value of a ComboBox

开发者 https://www.devze.com 2023-01-27 18:11 出处:网络
I\'m developing an AIR app that requires a menu to show only during the first run. In it the user will be able to choose the desired language for the app to run in.

I'm developing an AIR app that requires a menu to show only during the first run. In it the user will be able to choose the desired language for the app to run in.

I'm displaying this menu without a problem but I need it to stay visible until the "select language" comboBox is changed and then return the sele开发者_JS百科cted choice's data value.

My problem is that I can't seem to figure out how to return a value only after the combo box is changed.

function promptFRMenu():String{
FRMenu.enabled = FRMenu.visible = true; //when I detect the app is running for the             
                                       // first time, the dialog box is enabled 
                                       // and made visible
var peferedLng:String = new String;

    FRMenu.language_CBox.addEventListener(Event.CHANGE, announceSelectedItem);
        function announceSelectedItem(e:Event):void {
                 FRMenu.enabled = FRMenu.visible=false;
                 peferedLng = e.target.selectedItem.data;
                -> return peferedLng;  
                //It is the 'parent' function that should return this value but 
               // only after it is selected
        }


}

I'd really appreciate any help. Cheers!


you can't return a function from an event handler. also a function can't return a function from a nested function like that. further, a function will return a value as soon as it's invoked - you can't defer it until an event occurs. and just a piece of advice, it's generally not good practice to nest named functions like that.

i'm not clear on the net result you want to achieve, but you probably want something closer to this:

var peferedLng:String = new String;
function promptFRMenu():String{
  FRMenu.enabled = FRMenu.visible = true;
}
FRMenu.language_CBox.addEventListener(Event.CHANGE, announceSelectedItem);
function announceSelectedItem(e:Event):void {
   FRMenu.enabled = FRMenu.visible=false;
   peferedLng = e.target.selectedItem.data;
  // do whatever you want to do with peferedLng here
}


This works for me, I am using FB Burrito w/the Hero SDK

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            private function _handleChange($event:Event) :void
            {
                trace($event.target + " // CHANGED // " + $event.target.selectedItem.value);
            }
        ]]>
    </fx:Script>

    <mx:ComboBox id="comboBox" rowCount="5" labelField="label" prompt="Select One" change="_handleChange(event);">
        <mx:dataProvider>
            <s:ArrayList>
                <fx:Object label="One" value="1" />
                <fx:Object label="Two" value="2" />
                <fx:Object label="Three" value="3" />
                <fx:Object label="Four" value="4" />
                <fx:Object label="Five" value="5" />
            </s:ArrayList>
        </mx:dataProvider>
    </mx:ComboBox>

</s:Application>
0

精彩评论

暂无评论...
验证码 换一张
取 消