我在AS3中完成任务时遇到了一些麻烦。我有一个名为Rand的var,如果这个rand介于0到10之间,它将显示死亡磁盘。如果兰德在90到100之间,则会出现王盘。我想要实现的目标是,这些磁盘是否永远不会连续出现两次。我尝试使用布尔但它没有用。除此之外,只有当“var”rand介于我提到的数字之间时,才能出现国王和死亡盘。有时它出现在兰特数字之外。我希望有人可以帮我解决这个问题。
一些信息:
磁盘是一个MC和一个按钮。这个MC磁盘中有8个帧,1到6个是普通磁盘,7和6个是普通磁盘。 8王和死亡盘。您可以将磁盘与骰子进行比较。当播放器按下Movieclip磁盘时,将选择一个随机帧。
diskRoll [class]:
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();
}
}
}
答案 0 :(得分:0)
SO。如果我试着用A和B解释得更清楚。 您希望A出现10%的案例。 你希望B出现80%的案例。 你希望C出现10%的情况。 你想永远不会获得AA或CC或AC。
现在,创建一个数组A和C的数组,它是最终数组的20%。通过应用排序函数来对其进行混洗,其条件是Math.random()&lt; 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 );