Actionscript 3 如何从类中获取随机常数?

Actionscript 3 如何从类中获取随机常数?,actionscript-3,Actionscript 3,可以从AS3中的类中获取一个随机常数吗 class Constat { public static const constA:String = "const1"; public static const constB:String = "const2"; ... /** this method must return a random constant */ public static function getRandomConst():String

可以从AS3中的类中获取一个随机常数吗

class Constat
{
    public static const constA:String = "const1";
    public static const constB:String = "const2";
    ...

    /** this method must return a random constant */
    public static function getRandomConst():String
    {
         -------------------------------
    }
}

是的,您可以将所有值粘贴到一个数组中,然后随机选择一个。

设置为常量名称,后跟一个数字。并使用字典方法与
Math.random()
结合使用

试试这个

package {

    import flash.display.Sprite;

    public class MyClass extends Sprite
    {
        public static const constA:String = "00";
        public static const constB:String = "11";
        public static const constC:String = "22";
        public static const constD:String = "33";
        public static const constE:String = "44";
        public static const constF:String = "55";
        public static const constG:String = "66";
        public static const constH:String = "77";
        public static const constI:String = "88";
        public static const constJ:String = "99";
                             .
                             .
                             .

        public function MyClass() 
        {   
            MyClass.test();
        }

        public static function mapped(i:int):String
        {
            //65 is A
            return String.fromCharCode(65+i);                   
        }

        public static function test():void
        {
            trace(MyClass["const"+mapped(int(Math.random()*10))]);
        }

    }
}
您可以使用收集类上定义的所有常量,然后从中随机选择一个

public class Constat
{

    public static const constA:String = "const1";
    public static const constB:String = "const2";

    private static var _constants:Vector.<String>;


    public static function getRandomConst():String
    {
        if(_constants === null)
        {
            _constants = new <String>[];

            var def:XML = describeType(Constat);

            for each(var i:XML in def.constant)
            {
                _constants.push(i.@name);
            }
        }


        // Select random.
        var con:String = _constants[ int(Math.random() * _constants.length) ];

        return Constat[con];
    }

}
公共类Constat
{
公共静态常量constA:String=“const1”;
公共静态常量constB:String=“const2”;
私有静态var_常量:向量。;
公共静态函数getRandomConst():String
{
如果(_常量===null)
{
_常数=新[];
var def:XML=descripbetype(Constat);
对于每个(变量i:def.constant中的XML)
{
_常量推送(i.@name);
}
}
//选择“随机”。
var con:String=_常量[int(Math.random()*_常量.length)];
返回常数[con];
}
}

你能写更多关于它的细节吗?还是代码示例?因为我没有找到任何关于如何做的信息。你以前和array合作过吗?这很直截了当。一旦你将值添加到数组中,你就可以生成一个随机索引,从数组中选择一个值。很抱歉,也许我没有理解你的答案(英语对我来说很难)。我认为有一种方法可以将这个类转换成一个带有常量的数组,然后得到随机值。(顺便说一句,我刚刚找到了如何做到这一点[获取类的常量数组](http://stackoverflow.com/questions/11596475/as3-how-can-i-get-an-array-of-a-constants-of-a-class)(但我认为这个解决方案很慢)在类中存储具有常量s名称的数组对我来说不是一个好的解决方案(如果我现在不相信你的提议)(但效果很好:))谢谢你的回答。你的解决方案很简单也很好,但是我的君士坦人的名字是不同的。支票,请。这个代码非常优雅。我的想法是​​映射数字和字母表。再次感谢你,但你不明白。。我写了康斯塔和康斯塔,但我做了个例子。。真正的名字是拉布拉多、罗威勒和e.t.cmitim给出了可以接受的答案,我在评论中写道,我找到了另一个好的解决方案-。我认为您的代码在以后的另一种情况下会很有用