动作脚本3计数器/按钮颜色更改

时间:2015-05-01 02:41:24

标签: actionscript-3 flash

我是Action Script的新手,需要帮助。

基本上,我有50个按钮,每个按钮的形状与美国的状态相对应。我的意图是这样,我将只有一个正确的答案显示问题(例如,哪个状态是阳光状态?)然后目标是点击正确的状态。

我的问题是保持得分,我认为我可以采取这两种方式,但我正在努力解决这两个问题。 第一种方式是计数器,如果你点击正确的状态向计数器添加一个点,如果你点击不正确的状态不添加一个点,但错误或正确,两次点击都会带你到下一个问题。这个问题是创建一个计数器,我尝试了很多东西,似乎无法让它工作。

另一个选项是点击正确状态会改变该状态绿色的颜色,点击错误状态会将正确状态的颜色更改为红色,同时两次点击仍然会带您进入下一个问题。我也试过这种方法,似乎无法弄清楚如何改变按钮的颜色。除了我声明的50个状态按钮之外,没有太多代码可以显示(即virginiabutton.addEventListener(MouseEvent.CLICK,virginiabuttonclick);

修改

所以我拿出了柜台,我不再需要它,因为颜色几乎正常。所以我遇到的问题是任何问题,如果你点击正确的状态,将正确的状态变为绿色并转到下一个问题。如果您单击错误的状态,请将正确的状态设置为红色并转到下一个问题。除了“错误点击”功能外,此代码完全正确。当我点击不正确的状态时,它只是将不正确的状态(在这种情况下为CA)变为红色而不是VA变为红色。我认为代码中的问题是在else函数中,它表示“e.currentTarget”。我希望它将当前正确的状态更改为红色而不是currrentTarget。

var currentCorrectState:DisplayObject; 

var correctColor:ColorTransform = new ColorTransform();
correctColor.color = 0x009900; //green

var wrongColor:ColorTransform = new ColorTransform();
wrongColor.color = 0x990000; //red

virginiabutton.addEventListener(MouseEvent.CLICK, buttonClick);
californiabutton.addEventListener(MouseEvent.CLICK, buttonClick);
// have just these two in for now just so i have a correct state(va) and incorrect state(ca)

currentCorrectState = virginiabutton
//va is correct state for this question

function buttonClick(e:Event):void {

    if(e.currentTarget == currentCorrectState){
       DisplayObject(e.currentTarget).transform.colorTransform = correctColor;
       gotoAndStop(3);
        //this code works, it transforms VA to green which is what I want. It also goes to the next question.

    }else{
       DisplayObject(e.currentTarget).transform.colorTransform = wrongColor;
        gotoAndStop(3);
        //this code doesn't work, it only transforms the currenTarget state(in this case CA) to red. it also goes to the next question which works. 
    }

}

1 个答案:

答案 0 :(得分:0)

以下是您可以尝试的示例:

var ctr:int = 0; //counter to hold the amount of correct gueses

var currentCorrectState:DisplayObject; //var for holding the current state that is correct.

var correctColor:ColorTransform = new ColorTransform();
correctColor.color = 0x009900; //green

var wrongColor:ColorTransform = new ColorTransform();
wrongColor.color = 0x990000; //red

virginiabutton.addEventListener(MouseEvent.CLICK, buttonClick);
//add the rest of click listeners, best to do it in a loop so it's not so tedious

//generic click handler for all buttons
function buttonClick(e:Event):void {
    //turn the correct state the correct color:
    currentCorrectState.transform.colorTransform = correctColor;

    //e.currentTarget is the button that was clicked
    if(e.currentTarget == currentCorrectState){
       ctr++; //increment the correct counter

       //answer was correct, so make the correct state green
       currentCorrectState.transform.colorTransform = correctColor;

       //do anything else you want to do if they answered correct
    }else{
       //answer was incorrect, make the correct state red
       currentCorrectState.transform.colorTransform = wrongColor;
       //do anything else you want to do when they answer incorrect
    }

    //if you have a score text field update it
    score.text = "Your Score: " + ctr;

    //go to the next question (however you do that)
    nextQuestion();

}

function nextQuestion():void {
    //ask your question, however you do that

    //if there was a previous question, reset the last states color
    if(currentCorrectState) currentCorrectState.transform.colorTransform = new ColorTransform();

    //set the correct answer for this new question
    currentCorrectState = virginiabutton; 
}