Actionscript 3 如何在彩票游戏中循环不同的数字? 公共类彩票扩展了Sprite{ 公共变量text0:TextField,text1:TextField,text2:TextField,text3:TextField,text4:TextField,text5:TextField; public-var-backImage:Sprite,luckyBtn:Sprite,clearBtn:Sprite; public var tfFormat:TextFormat=新的TextFormat; 公共功能彩票() { setTextFieldFormat(); loadGUI(); } 公共功能luckyDip(事件:MouseeEvent):无效{ 对于(var i:uint=0;i

Actionscript 3 如何在彩票游戏中循环不同的数字? 公共类彩票扩展了Sprite{ 公共变量text0:TextField,text1:TextField,text2:TextField,text3:TextField,text4:TextField,text5:TextField; public-var-backImage:Sprite,luckyBtn:Sprite,clearBtn:Sprite; public var tfFormat:TextFormat=新的TextFormat; 公共功能彩票() { setTextFieldFormat(); loadGUI(); } 公共功能luckyDip(事件:MouseeEvent):无效{ 对于(var i:uint=0;i,actionscript-3,random,Actionscript 3,Random,在彩票游戏中,您的结果数量有限,例如:1-50之间的5个数字。您只需创建1-N之间的随机唯一数字列表 public class Lottery extends Sprite { public var text0:TextField, text1:TextField, text2:TextField, text3:TextField, text4:TextField, text5:TextField; public var backImage:Sprite, luckyBtn:S

在彩票游戏中,您的结果数量有限,例如:1-50之间的5个数字。您只需创建1-N之间的随机唯一数字列表

public class Lottery extends Sprite {

    public var text0:TextField, text1:TextField, text2:TextField, text3:TextField, text4:TextField, text5:TextField;
    public var backImage:Sprite, luckyBtn:Sprite, clearBtn:Sprite;
    public var tfFormat:TextFormat = new TextFormat;

    public function Lottery()
    {
        setTextFieldFormat();
        loadGUI();
    }

    public function luckyDip(event:MouseEvent):void {
        for (var i:uint = 0; i <= 49; i++) {
            this["text" + i].text = Math.ceil(Math.random() * 49);
        }
    }

    public function resetFields(event:MouseEvent):void {
        for (var i:uint = 0; i <= 49; i++) {
            this["text" + i].text = "";
        }
    }
//如何使用,结果之一:9,40,44,29,4
跟踪(乐透发生器(5));
私有函数lotteryGenerator(结果:uint,最大值:uint=50):数组{
变量i:uint,幸运数字:uint,结果:Array=[],添加:uint;
while(添加<结果){
luckyNumber=1+Math.random()*(maxValue-1);
if(result.indexOf(luckyNumber)==-1){
结果。推送(幸运数字);
增加++;
}
}
返回结果;
}

通常的方法是列出每个数字,然后随机删除它们。在数字上使用a。然后按顺序获取/使用数字。这将确保合理的分布,并防止任何重复选择。可能的重复(我投了重复票,因为创建初始序列,然后使用洗牌结果大多是无关紧要的任务细节;还有一些通过搜索“actionscript shuffle”很容易找到的具体问题。)谢谢,我是个初学者,所以无法将其应用到我的项目中!将/pass
lotteryGenerator
函数复制到
lotket
类中并使用它。函数返回唯一的数字数组。再次感谢,但我无法从框中获取数字!
//How to use, one of the results: 9,40,44,29,4
trace(lotteryGenerator(5));

private function lotteryGenerator(results: uint, maxValue:uint = 50):Array{
    var i: uint, luckyNumber: uint, result: Array = [], added: uint;

    while(added < results){
        luckyNumber = 1 + Math.random() * (maxValue - 1);
        if(result.indexOf(luckyNumber) == -1){
            result.push(luckyNumber);
            added++;
        }
    }

    return result;
}