as3中的随机文本生成器?

时间:2013-10-29 04:32:01

标签: actionscript-3 flash

我希望有一个动态文本字段,它将生成我指定的不同文本。我怎样才能做到这一点?我正在使用actionscript 3

2 个答案:

答案 0 :(得分:4)

如果你想要随机字符试试这个:

function generateRandomString(strlen:Number):String{
  var chars:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var num_chars:Number = chars.length - 1;
  var randomChar:String = "";

  for (var i:Number = 0; i < strlen; i++){
  randomChar += chars.charAt(Math.floor(Math.random() * num_chars));
  }
  return randomChar;
}

trace(generateRandomString(7));

答案 1 :(得分:0)

var array:Array = new Array ("Apples","Bananas","Grapes"); //create an array of possible strings
var randomIndex:int = Math.floor ( Math.random () * array.length ); //generate a random integer between 0 and the length of the array
myTextField.text = array [ randomIndex ]; //put the random string in your text field