热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在[“随机数”]代码中加载随机数-Howtoloadanrandomnumberin[“randomnumber”]code

Hithisismycocdetoloadanxmlfile:嗨这是我加载xml文件的cocde:varxmlLoader:URLLoadernewURLLoader

Hi this is my cocde to load an xml file:

嗨这是我加载xml文件的cocde:

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
 this.url = url;
xmlLoader.load(new URLRequest("C:\Documents and Settings\...books.xml"));

function LoadXML(e:Event):void {
    xmlData = new XML(e.target.data);
    trace(xmlData.author.text()[0]);
}

but I want to generate a random number in this line trace(xmlData.author.text()[0]); so instead of [0] there needs to be an random number between the [ ]

但我想在这一行跟踪中生成一个随机数(xmlData.author.text()[0]);所以代替[0],[]之间需要一个随机数

1 个解决方案

#1


0  

Continuation from comments; you can't just copy and paste your way through a problem - you have to apply a little thought...

继续评论;你不能只是复制和粘贴你的问题 - 你必须应用一点思考......

// break down the problem, if this line returning 
// an array lets assign it to a variable.
var authorText = xmlData.author.text();

// random index should be within the bounds of the array length
var random = Math.floor( Math.random() * authorText.length );

// put it together
trace( authorText[random] );

Assuming the rest of your code is okay then this should work - here's a demo in Javascript though I suspect you are doing something in actionscript / flash (though they both conform to ECMAScript so it should work just the same).

假设你的其余代码还可以,那么这应该可行 - 这里是Javascript的演示,虽然我怀疑你在动作脚本/ flash中做了一些事情(虽然它们都符合ECMAScript所以它应该工作相同)。


推荐阅读
author-avatar
plumscape_191
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有