1

我在 AS3 中完成任务时遇到了一些麻烦。我有一个名为 Rand 的变量,如果这个 rand 介于 0 - 10 之间,它将显示死亡磁盘。如果 rand 介于 90 - 100 之间,则会出现金盘。我想要实现的目标是那些磁盘永远不会连续出现两次。我尝试使用布尔值,但它没有用。除此之外,只有当“var”rand 介于我提到的数字之间时,国王和死盘才会出现。有时它出现在兰特数之外。我希望有人可以帮助我解决这个问题。

一些信息:

disks 是一个 MC 和一个按钮。这个 MC 磁盘有 8 个框架,其中 1 - 6 个是普通磁盘,7 和 8 个是国王和死亡磁盘。您可以将磁盘与骰子进行比较。当播放器按下磁盘 Movieclip 时,将选择一个随机帧。

diskRoll [类]:

package  {

    import flash.display.*;
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;
    import flash.media.*;

    public class DiskRoll extends MovieClip 
    {
        //Vars

        var diskTilt:Timer = new Timer(1350,1);
        //var rand:int = Math.random() * 100;
        var rand:int = (Math.random()*(100));



        var deathDisk:Boolean = false;
        var kinghDisk:Boolean = false;

        //Sound Vars

        var diskFxChannel:SoundChannel = new SoundChannel();
        var diskHitFx:Sound = new diskHitsound();   
        var diskRollFx:Sound = new diskRollSound(); 

        public function DiskRoll():void 
        {           
            this.addEventListener(MouseEvent.MOUSE_DOWN, hitDisk);
            diskTilt.addEventListener(TimerEvent.TIMER_COMPLETE, tiltOnComplete);
            diskFxChannel = diskRollFx.play();
        }

        //Disk hit function

        private function hitDisk(event:MouseEvent):void{

            //Picks random frame after MOUSE_DOWN from Disks Mc [1-8 Frames total]
            this.gotoAndStop(Math.ceil(Math.random()* 6));

            //Genarate random number calc chances and luck
            rand = (Math.random() * (100));

            //Disk of death between Random number (block second time)
            if(rand >= 90 && rand <=100){ 
                this.gotoAndStop(7);
                kinghDisk = true;
            }

            if(rand >= 0 && rand <=10){ 
                this.gotoAndStop(8);
                deathDisk = true;
            }

            //Disk of death between Random number (block second time)
            if(deathDisk == true){
                rand = (Math.random() * (100))+11;
            }

            if(deathDisk == false){
                rand = (Math.random() * (100));
            }

            //KingsDisk Random between number (block second time)           
            if(kinghDisk == true){
                rand = (Math.random() * (89));
            }

            if(kinghDisk == false){
                rand = (Math.random() * (100));
            }                   

            //If statements per Disk
            if(this.currentFrame == 1){
                trace("DiskRoll 1")
                deathDisk = false;
                kinghDisk = false;
            }
            if(this.currentFrame == 2){
                trace("DiskRoll 2")
                deathDisk = false;
                kinghDisk = false;              
            }
            if(this.currentFrame == 3){
                trace("DiskRoll 3")
                deathDisk = false;
                kinghDisk = false;              
            }
            if(this.currentFrame == 4 /*&& stage.getChildByName("block") == null*/){
                trace("DiskRoll 4")
                deathDisk = false;
                kinghDisk = false;              
            }
            if(this.currentFrame == 5){
                trace("DiskRoll 5")
                deathDisk = false;
                kinghDisk = false;              
            }
            if(this.currentFrame == 6){
                trace("DiskRoll 6")
                deathDisk = false;
                kinghDisk = false;              
            }
            if(this.currentFrame == 7){
                trace("DiskRoll 7")
                deathDisk = false;
                kinghDisk = true;               
            }
            if(this.currentFrame == 8){
                trace("DiskRoll 8")
                deathDisk = true;
                kinghDisk = false;              
            }

            trace(rand);
            trace(deathDisk);
            trace(kinghDisk);

            diskFxChannel = diskHitFx.play();

            this.removeEventListener(MouseEvent.MOUSE_DOWN, hitDisk);

            diskTilt.start();

        }

        //Deblock Disk (button) Timer
        private function tiltOnComplete(event:TimerEvent):void {

            this.gotoAndPlay(1);
            diskTilt.stop();
            this.addEventListener(MouseEvent.MOUSE_DOWN, hitDisk);

            diskFxChannel = diskRollFx.play();

        }

    }

}
4

1 回答 1

0

所以。如果我尝试用 A 和 B 解释得更清楚。您希望 A 出现 10% 的案例。您希望 B 出现 80% 的案例。您希望 C 出现 10% 的案例。您永远不想获得 AA、CC 或 AC。

现在,用元素 A 和 C 创建一个数组,占最终数组的 20%。通过应用一个条件为 Math.random() < 0.5 的排序函数来对其进行洗牌。

现在,在每 2 个元素之间插入 1 个元素 B。从最终数组的 80% 开始,您应该有足够的空间。这将确保没有两个磁盘是连续的。

现在将其余的 B 插入任何你想要的地方,没关系。当然,随机做。只需 Math.random() *temporaryArray.length 即可在数组中获取随机索引来插入它。

如果有任何问题或需要澄清,请拍:)

编辑

//used some string to identify them. you use your instances
        var deathDisk : String = "death";
        var kingDisk  : String = "king";
        var normalDisk : String = "normal";

        //Any nr of total disks. I reserve 10% of death and 10% of king. The rest are normal.
        var totalNrOfDisks : Number = 40;
        var nrOfDeath : uint = totalNrOfDisks * 10 / 100;
        var nrOfKing  : uint = totalNrOfDisks * 10 / 100;

        //Generate all kings and deaths.
        var kingAndDeath : Array = [];
        for( var i : int = 0; i < nrOfDeath; i++ ) 
        {
            kingAndDeath.push( deathDisk );
        }
        for( i  = 0; i < nrOfKing; i++ ) 
        {
            kingAndDeath.push( kingDisk );
        }

        //shuffle them
        kingAndDeath.sort( function( element0:Number, element1:Number ):Boolean
        {
            return Math.random() < 0.5;
        });

        //Put at least 1 normal between them.
        var allArray : Array = [];
        for( i  = 0; i < kingAndDeath.length; i++ ) 
        {
            allArray.push( normalDisk );
            allArray.push( kingAndDeath[ i ] );
        }


        //While there is room for normals (max nr of disks not attained), insert one at a random spot in the array.
        while( allArray.length < totalNrOfDisks )
        {
            var randomIndex : uint = Math.random() * allArray.length;

            allArray.splice( randomIndex, 0, normalDisk );
        }
        trace( allArray );
于 2013-05-30T11:04:34.543 回答