2015-09-21 68 views
1

嘿大家我已经看到一些关于这个论坛,但似乎无法弄清楚仍然。如何获得随机生成的数字没有重复

所以我有一个名为aClockArray的数组,这是一个自定义数组,里面有4个影片剪辑。嵌套的影片剪辑对每个帧都有不同的颜色。我已在阵列设置像这样在我的构造函数:

aClockArray = [playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8]; 
在另一个函数

然后,我有一个for循环建立通过所有对象的数组进行迭代,并让他们gotoAndStop在他们的嵌套影片一个随机帧它夹

private function randomColorGenerator():void 
    { 
     //Loop through wires and make them randomn generate color 
     for (var i:int = 0; i < aClockArray.length; i++) 
     { 
      var currentWires = aClockArray[i]; 

      nWire = randomNumber(2, 7); 

      currentWires.gotoAndStop(nWire); 


     } 
    } 

现在这个完美的作品,我也得到随机颜色我每次重启时间:2-7我有它随机像这样去。但我想要完成的是颜色不要重复,所以不要重复2至7个数字。我会如何去做这些事情,让这些数字随机生成而不是重复?

而且这里是我的randomNumber功能:

//Generates a truly "random" number 
    function randomNumber(low:Number=0, high:Number=1):Number 
    { 
     return Math.floor(Math.random() * (1+high-low)) + low; 
    } 

感谢您的帮助,将不胜感激!

尝试过这样的事情,但仍然有重复:(

//Loop through wires and make them randomn generate color 
     for (var i:int = 0; i < aClockArray.length; i++) 
     { 
      var currentWires = aClockArray[i]; 
      var frames:Array = [2, 3, 4, 5, 6, 7, 8]; 
      var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1); 


      currentWires.gotoAndStop(randomFrame); 


     } 

回答

3

创建唯一可能的结果的数组:

var frames:Array = [2, 3, 4, 5, 6, 7]; 

然后从该数组在随机指数与splice删除:

var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1); 
+0

感谢您对本病的让你知道如何去后的今天,当我尝试一下! – Nathan

+1

它可能会略有* *更快第一随机排列,然后用'POP()'或'移()'(什么@Flashist是*试图*完成),不过这也是一个很好的解决方案和最紧凑。除非你每帧都在运行代码,否则不应该有任何性能差异。 – BadFeelingAboutThis

+0

仍然困惑我将如何实现这一点。我仍然会用我目前的Loop吗?添加这样的东西,并从那里拼接? – Nathan

-3

我不会给你确切的代码,因为我不写在一个ctionscript。但是,你只需要保存你使用的最后一种颜色,并避免它:

{next_color = randomNumber(2,7)} until next_color != last_color; 
currentWires.gotoAndStop(next_color); 
last_color = next_color; 

中提琴!伪随机颜色。

+0

有趣谢谢你的建议,我一定会尝试今天! – Nathan

+0

确保第一个和第三个号码不同吗? – null

+0

这不是一个正确的解决方案。 – BotMaster

3

我建议是这样的:

  1. 创造价值的预定义列表
  2. 随机化列表
  3. 移列表的第一个值在每个迭代

例如:

import flash.display.MovieClip; 

    function randomizeSort(obj1:Boolean, obj2:Object):int 
    { 
     var randNum:int = -1 + Math.floor((Math.random() * 3)); 
     return randNum; 
    } 

    var framesList:Vector.<int> = Vector.<int>([2, 3, 4, 5, 6, 7]) 
    framesList.sort(randomizeSort); 
    trace(framesList); 

    var tempColor:int; 
    var clipsList:Vector.<MovieClip> = Vector.<MovieClip>([playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8]); 
    var clipsCount:int = clipsList.length; 
    for (var clipIndex:int = 0; clipIndex < clipsCount; clipIndex++) 
    { 
     tempColor = framesList.shift(); 
     trace(tempColor); 
    } 
+0

@BadFeeling关于这不是构造函数,而是[顶层函数Vector。 (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/package.html#Vector%28%29)另一个速记'VECTOR'符号是['新 [1,2,3 ]'](http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee5.html#WSB1F41227-C612-4f33-A00E-CE84C1913E1C) – null

+0

@null - 我站在纠正。猜猜我需要更频繁地使用速记。并不是说我在一年的较好时间里甚至已经完成了一个AS3项目。 – BadFeelingAboutThis

1

这是原型设计的理想人选:

MovieClip.prototype.gotoRandomFrame = function():void 
{ 
    if(!this.frameReferences) 
    { 
     this.frameReferences = []; 
     for(var i:int = 0; i < this.totalFrames; i++) 
     { 
      this.frameReferences.push(i + 1); 
     }  
    } 
    var index:int = this.frameReferences.splice(Math.floor(Math.random() * this.frameReferences.length), 1); 
    if(!this.frameReferences.length) 
    { 
     this.frameReferences = null;   
    } 
    this.gotoAndStop(index); 
} 

THX这个原型,你现在可以调用任何mocieclip这种方法,让他们展示自己的时间表独特的帧,直到他们已经显示他们所有,他们重新开始。

mymovie.gotoRandomFrame(); 

其他答案是正确的,但它们没有考虑到多个动画片段的情况。必须创建与影片剪辑一样多的代码和数组,这将是很糟糕的。

+1

如果您编辑原型,您将为每个实例创建一个数组(无论您是否需要该功能)。最好创建一个类定义,并将其扩展到其他Class或将其设置为MovieClip符号的Class。 –

+0

@AmyBlankenship我不喜欢或者使用原型,但说句公道话,'frameReferences'阵列仅被连接,如果你调用'gotoRandomFrame()'方法。 – Aaron

+0

如何实现这一点仍然有点混乱。仍然没有运气去它。试过Nulls方法但没有运气 – Nathan