2014-01-27 65 views
1

我想製作一個Flash應用程序,它隨機選取並在四個變量之間給它一個值。 所以我寫了這個代碼:隨機挑選變量並賦予它們一個值AS3

import flash.events.Event; 

var max:int = 100; 
var red:int = 0; 
var yellow:int = 0; 
var orange:int = 0; 
var blue:int = 0; 

function updateChart():void 
{ 
var total:int = red + yellow + orange + blue; 
redbar.height = red/total * max; 
yellowbar.height = yellow/total * max; 
orangebar.height = orange/total * max; 
bluebar.height = blue/total * max; 

redres_txt.text = String(red); 
yellowres_txt.text = String(yellow); 
orangeres_txt.text = String(orange); 
blueres_txt.text = String(blue); 
total_txt.text = "Total: " + String(total); 
} 
Vote_Button.addEventListener(MouseEvent.CLICK, onRedClick); 
function onRedClick(evt:MouseEvent):void 
{ 
var randomSelector:Array = [red, yellow, orange, blue]; 
var random:* = randomSelector[Math.floor(randomSelector.length * Math.random())]; 
random++; 
updateChart(); 
} 

這是整個代碼。問題是;當我點擊按鈕時,數字返回到零,其他點擊不會影響。 fla文件在這裏,如果你想看看它: https://drive.google.com/file/d/0B2f6Y60ccirBamt1RThKV2VqSjg/edit?usp=sharing

謝謝。

回答

3

更改onRedClick處理程序是這樣的:

function onRedClick(evt:MouseEvent):void 
{ 
    var randomSelector:Array = ["red", "yellow", "orange", "blue"]; 
    var random:String = randomSelector[randomSelector.length * Math.random() | 0]; 
    this[random]++; 
    updateChart(); 
} 
+0

它的工作謝謝! –

+0

@ KolonE.T。別客氣! – Cherniv