換弦

2012-02-24 31 views
0

以及傢伙,我這裏有一個小問題,我有3個集裝箱,並與n個節點的XML ...我想放在容器中,XML的任何值,該值不能是一箇舊值和容器不應該具有相同的值...我在AS3這個代碼,但它不工作:S換弦

package 
{ 
    import flash.display.MovieClip; 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.text.TextField; 



    public class Pruebas extends Sprite 
    { 

     public var arr:Array = new Array("valor0","valor1","valor2","valor3","valor4","valor5") 


     public var str1:String=arr[0]; 
     public var str2:String=arr[1]; 
     public var str3:String=arr[2]; 




     public var te1:TextField = new TextField(); 
     public var te2 :TextField = new TextField(); 
     public var te3 :TextField = new TextField(); 

     public var btn :MovieClip = new MovieClip(); 




     public function Pruebas() 
     { 
     te1.text = str1;  
     te2.text = str2; 
     te3.text = str3; 

     addChild(te1); 
     addChild(te2); 
     addChild(te3); 

     te2.x = 50;te3.x=100; 

     btn.graphics.beginFill(0xff00ff); 
     btn.graphics.drawCircle(151,150,15); 
     addChild(btn); 

     btn.addEventListener(MouseEvent.CLICK,cambiar); 
     } 


     public function cambiar(e:Event):void{ 
     var minLimit:uint = 0; 
     var maxLimit:uint = arr.length-1; 
     var range:uint = maxLimit - minLimit; 

     var someNum:Number = Math.ceil(Math.random()*range) + minLimit;  


     if(str1 == arr[someNum]){ 
      while(str1 == arr[someNum]){ 
       someNum = Math.ceil(Math.random()*range) + minLimit; 
      } 
      str1 = arr[someNum]; 
     }else{ 
      str1 = arr[someNum]; 
     } 
     someNum= Math.ceil(Math.random()*range) + minLimit;  

     if(str2 == arr[someNum]){ 
      while(str2 == arr[someNum]){ 
       someNum = Math.ceil(Math.random()*range) + minLimit; 
      } 
      str2 = arr[someNum]; 
     }else{ 
      str2 = arr[someNum]; 
     } 

     someNum= Math.ceil(Math.random()*range) + minLimit;  

     if(str3 == arr[someNum]){ 
      while(str3 == arr[someNum] || str3 ==str1 || str3 == str2){ 
       someNum = Math.ceil(Math.random()*range) + minLimit; 
      } 
      str3 = arr[someNum]; 
     }else{ 
      str3 = arr[someNum]; 
     } 




     te1.text = str1;  
     te2.text = str2; 
     te3.text = str3; 
     } 


    } 
} 

愛德華檢查這個代碼,這個代碼我做

if(imgactual == myXML.internas.item[someNum][email protected]){ 
       while(imgactual == myXML.internas.item[someNum][email protected]){ 
        someNum = Math.ceil(Math.random()*range) + minLimit; 
       } 
       imgactual = myXML.internas.item[someNum][email protected] 
      }else{ 
       imgactual = myXML.internas.item[someNum][email protected] 
      } 
      someNum= Math.ceil(Math.random()*range) + minLimit; 

      if((imgpostactual == myXML.internas.item[someNum][email protected]) || (imgpostactual == imgactual)){ 
       while(imgpostactual == myXML.internas.item[someNum][email protected] || imgpostactual == imgactual){ 
        someNum = Math.ceil(Math.random()*range) + minLimit; 
       } 
       imgpostactual = myXML.internas.item[someNum][email protected] 
      }else{ 
       imgpostactual = myXML.internas.item[someNum][email protected] 
      } 

      someNum= Math.ceil(Math.random()*range) + minLimit; 

      if((imgpreactual == myXML.internas.item[someNum][email protected]) || (imgpostactual == imgpreactual) || (imgpreactual == imgactual)){ 
       while((imgpreactual == myXML.internas.item[someNum][email protected]) || (imgpostactual == imgpreactual) || (imgpreactual == imgactual)){ 
        someNum = Math.ceil(Math.random()*range) + minLimit; 
       } 
       imgpreactual = myXML.internas.item[someNum][email protected]; 
      }else{ 
       imgpreactual = myXML.internas.item[someNum][email protected]; 
      } 
      trace(imgactual); 
      trace(imgpreactual); 
      trace(imgpostactual); 

這段代碼的壞處是總是循環而t碟剎很長一段時間:S和我認爲它不工作:S

回答

0

另一種解決方案是檢查如果第二選擇是相同的對象作爲第一,第三選擇是相同的對象作爲第一個和第二個,如果它們中的任何一個匹配,則抓取不同的隨機對象。

喜歡的東西:

var index1:int = (int)(Math.random() * arr.length); 
var index2:int = index1; 
while (index2 == index1) { 
    index2 = (int)(Math.random() * arr.length); 
} 
var index3:int = index1; 
while (index3 == index1 || index3 == index2) { 
    index3 = (int)(Math.random() * arr.length); 
} 

str1 = arr[index1]; 
str2 = arr[index2]; 
str3 = arr[index3]; 

一個do while循環非常適合這個......我不記得的語法副手,雖然。

1

基本上,你想選擇3個不同的隨機數字0和MAXLIMIT之間,然後挑數組的元素在這些索引位置。一種做法是給每個索引一個平等的機會,就是用你的子索引創建一個數組,然後選擇前三個元素。是這樣的:

// Create the array with the indexes 
var idxArray:Array = new Array(); 
for(var i:int = 0; i<=maxLimit; i++) idxArray.push(i); 

// Shuffle it 
for(var j:int = 0; j<=someNumberBigEnough; j++){ 
    var idx1:int = Math.ceil(Math.random()*range) + minLimit; 
    var idx2:int = Math.ceil(Math.random()*range) + minLimit; 
    // to shuffle, swap elements 
    var aux:int = idxArray[idx1]; 
    idxArray[idx1] = idxArray[idx2]; 
    idxArray[idx2] = aux; 
} 

// Pick the first thee 
str1 = arr[idxArr[0]] 
str2 = arr[idxArr[1]] 
str3 = arr[idxArr[2]] 

其中someNumberBigEnough可以是,例如,等於range

+0

嗯OKü洗牌陣列的位置是確定......但在其他的循環?想象一下,每1分鐘更換一次,每次都必須在數組中有不同的值:S每當他們改變時,都是我的問題總是有人與其他人相等:S – 2012-02-26 16:38:34

+0

您可以重新洗牌,也可以將數組中的以下3個元素。 – Eduardo 2012-02-26 16:55:18

+0

你在我的代碼中看起來有什麼錯誤嗎?語法或邏輯我不知道或爲你我的代碼工作? – 2012-02-27 20:43:00