我想创建一个flash应用程序,它随机选择并在四个变量之间给它一个值。 所以我写了这段代码:
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
感谢。
答案 0 :(得分: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();
}