flex - 如何在单击时将按钮的文本更改为随机xml条目?

时间:2011-12-31 03:38:12

标签: xml flex flash-builder

我昨天发布了一个这样的问题,但我在说明中并不清楚....我有一个应用程序我正在制作flex我想要一个按钮的文本更改为xml中的随机条目单击按钮时.... xml位于assets文件夹中,标题为games.xml。我希望在按下按钮时从XML中选择随机游戏名称。

这里是games.xml

<games>
<game> blah blah game name 1
<description> description1 </description>
</game>
<game> some more blah blah game name 2
<description> description2 </description>
</game>
<game> insert GameName3 here
<description> description3 </description>
</game>
</games>

这是flex文件

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
backgroundColor="#0000FF" title="games!">

<fx:Script>
<![CDATA[

    protected function button1_clickHandler(event:MouseEvent):void
    {
        // TODO Auto-generated method stub
        gamebutton.label="test"   <---i want this to be a random game name
    }
]]>
</fx:Script>

<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button id="gamebutton" click="button1_clickHandler(event)" horizontalCenter="0" top="10" x="0" width="95%" label="Pick A Game"/>

</s:View>

感谢您提供的任何帮助!

2 个答案:

答案 0 :(得分:1)

试试这个:

<?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" >

    <fx:Script>
        <![CDATA[
            private var games:XML=<games>
<game> <gname>blah blah game name 1</gname>
<description> description1 </description>
</game>
<game><gname> some more blah blah game name 2</gname>
<description> description2 </description>
</game>
<game><gname> insert GameName3 here</gname>
<description> description3 </description>
</game>
</games>;
private var labelsArray:Array= [];
    private function something():void
    {
        games.game.gname.(labelsArray.push(toString()));
        var randNum:Number=Math.floor(Math.random()*labelsArray.length-1)+1;
        gamebutton.label=labelsArray[randNum].toString();
    }       
        ]]>

    </fx:Script>
            <s:Button label="click" id="gamebutton" click="something()"/>
</s:Application>

答案 1 :(得分:0)

应用程序完成后,我使用一个数组保存游戏的名称。当你单击按钮时,找出一个应该小于数组长度的随机数,然后让按钮的标签等于数组[randomIndx]。那么,你可以得到一个随机的名字。 我的test.mxml如下,它运作良好:D

 <?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"
        backgroundColor="#0000FF" applicationComplete="init()">

    <fx:Script>
        <![CDATA[

            private var games:Array = [];

            protected function init():void
            {
                for each(var x:XML in gameXml.children())
                {
                    games.push(String(x.@name));
                }
            }

            protected function button1_clickHandler(event:MouseEvent):void
            {
                var rIndex:int = Math.round(Math.random()*(games.length-1));
                gamebutton.label = games[rIndex];   
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <fx:XML id="gameXml" source="assets/games.xml" format="e4x"/>
    </fx:Declarations>
    <s:Button id="gamebutton" click="button1_clickHandler(event)" horizontalCenter="0" top="10" x="0" width="95%" label="Pick A Game"/>

    </s:Application>