从具有一定概率的阵列选择随机信息并把它添加到阶段 [英] Select random item from an array with certain probabilities and add it to the stage

查看:137
本文介绍了从具有一定概率的阵列选择随机信息并把它添加到阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其相当大的任务,但不适试图解释。

Its quite a big task but ill try to explain.

我有200个字符串列表一个数组,我希望能够随机选择一个,并把它添加到使用code阶段。我的影片剪辑ActionScript导出使用相同的类名作为数组中的字符串。另外,如果有可能,我会能够选择与predictability字符串,如第一个拥有0.7的机会第二个是0.1等。这里是我目前

I have an array with a list of 200 strings and I want to be able to randomly select one and add it to the stage using code. I have movieclips exported for actionscript with the same class name as the strings in the array. Also, if it is possible, would I be able to select the strings with predictability such as the first has a 0.7 chance the second a 0.1 etc. Here is what i have currently

var nameList:Array=["Jimmy","Bob","Fred"]

var instance:DisplayObject = createRandom(nameList);
addChild(instance);

function createRandom(typeArray:Array):*
{
// Select random String from typeArray.
var selection:String = typeArray[ int(Math.random() * typeArray.length) ];

// Create instance of relevant class.
var Type:Class = getDefinitionByName(selection) as Class;

// Return created instance.
return new Type();
}

这一切都引发了我这个错误

All this throws me this error

的ReferenceError:错误#1065:变量[麦类]没有定义

ReferenceError: Error #1065: Variable [class Jimmy] is not defined.

香港专业教育学院寻找类似的其它线程但没有结合随机,predictability和的addChild()的三项具体任务

Ive searched for other threads similar but none combine the three specific tasks of randomisation, predictability and addChild().

推荐答案

我认为你有两个问题:一个语言的问题和逻辑问题。在连接到您的code以上的.fla文件,在库中找到重新$ P $每个符号psenting的名称和写入了AS联动列该符号相关联的名称 - 例如,鲍勃,弗雷德 - 只是名字,没有标点符号。

I think that you've got two problems: a language problem and a logic problem. In the .fla connected to your code above, in the Library find each symbol representing a name and write into the 'AS linkage' column for that symbol the associated name -- e.g., 'Bob,' 'Fred' -- just the name, no punctuation.

现在 getDefinitionByName()会发现你的类

如果你把不同的图形到每一个影片剪辑 - 比方说,一个水果或鲍勃,吉姆,弗雷德的照片 - 并运行程序,你会一次比一次随机的东西在舞台上。

If you put a different graphic into each MovieClip -- say, a piece of fruit or a picture of Bob,Jim, Fred -- and run your program you'll get a random something on stage each time.

这应该解决您的语言问题。但逻辑问题是有点困难,不是吗? 这就是为什么我指出你Kelly先生的解决方案(第一位的,这对我来说更容易掌握)。

That should solve your language problem. But the logic problem is a little harder, no? That's why I pointed you to Mr. Kelly's solution (the first one, which for me is easier to grasp).

这篇关于从具有一定概率的阵列选择随机信息并把它添加到阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆