flex-单击时将按钮文本更改为随机xml条目
我有一个我正在用flex制作的应用程序,我想在单击按钮时,将按钮的文本更改为xml中的随机条目……xml位于资产文件夹中,标题为games.xml。我想有一个随机游戏被选中时,按下按钮 下面是games.xmlflex-单击时将按钮文本更改为随机xml条目,xml,apache-flex,random,flash-builder,Xml,Apache Flex,Random,Flash Builder,我有一个我正在用flex制作的应用程序,我想在单击按钮时,将按钮的文本更改为xml中的随机条目……xml位于资产文件夹中,标题为games.xml。我想有一个随机游戏被选中时,按下按钮 下面是games.xml <games> <game> GameName1 <description> description1 </description> </game> <game> GameName2
<games>
<game> GameName1
<description> description1 </description>
</game>
<game> GameName2
<description> description2 </description>
</game>
<game> GameName3
<description> description3 </description>
</game>
</games>
游戏名称1
说明1
游戏名称2
说明2
游戏名称3
说明3
这是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>
谢谢你能提供的任何帮助 Hi只需将以下代码放入button1\u clickHandler
protected function button1_clickHandler(event:MouseEvent):void
{
var num:int = (Math.random() * (4 - 1)) + 1
// TODO Auto-generated method stub
gamebutton.label="GameName" + num.toString();
}
这里4是最大游戏+1,1是最小值,num是介于1到3之间的随机数
祝您玩得愉快。在最终版本的代码中,游戏将有不同的名称,而不仅仅是数字占位符。有没有办法让它从xml中提取?抱歉,我一开始不清楚在最终版本的代码中,游戏将有不同的名称,而不仅仅是数字占位符。有没有办法让它从xml中提取?很抱歉,我一开始不清楚,你会得到数组,你只需要用给定的索引号为它提供te索引。你有一个数组名游戏,你只需要通过索引即games[num]。。。。也许这是有史以来最简单的方法。