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

Actionscript 3 动作脚本3计数器/按钮颜色更改,actionscript-3,flash,Actionscript 3,Flash,我不熟悉动作脚本,需要帮助 基本上,我有50个按钮,每个按钮的形状都和它对应的美国州一样。我的意图是这样的,我将有一个只有一个正确答案的问题显示(例如,哪个州是阳光州?),目标是点击正确的州 我的问题是保持分数,我想我可以从两个方面来做,但我正在努力做到这两个方面。 第一种方法是计数器,如果您单击正确的状态向计数器添加点,如果您单击不正确的状态不添加点,但单击错误或正确,两次单击都将带您进入下一个问题。这个问题是创建一个计数器,我已经尝试了许多方法,但似乎无法使其工作 另一个选项是,单击正确的状

我不熟悉动作脚本,需要帮助

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

我的问题是保持分数,我想我可以从两个方面来做,但我正在努力做到这两个方面。 第一种方法是计数器,如果您单击正确的状态向计数器添加点,如果您单击不正确的状态不添加点,但单击错误或正确,两次单击都将带您进入下一个问题。这个问题是创建一个计数器,我已经尝试了许多方法,但似乎无法使其工作

另一个选项是,单击正确的状态会将该状态的颜色更改为绿色,而单击错误的状态会将正确状态的颜色更改为红色,两次单击仍会将您带到下一个问题。我也尝试过这种方法,但似乎不知道如何更改按钮的颜色。除了我声明的50个状态按钮(即virginiabutton.addEventListener(MouseEvent.CLICK,virginiabuttonclick))之外,没有多少代码可以显示

编辑

所以我拿出了计数器,我真的不再需要它了,因为颜色都差不多了。所以我遇到的问题是任何问题,如果你点击正确的状态,把正确的状态变成绿色,然后进入下一个问题。如果你点击错误的状态,把正确的状态变成红色,然后进入下一个问题。这段代码正确y除了“错误点击”函数外。当我点击错误状态时,它只是将错误状态(在本例中为CA)变为红色,而不是VA变为红色。我认为代码中的问题在于else函数中的“e.currentTarget”.我希望它将当前的正确状态改为红色,而不是CurrentTarget

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. 
    }

}

下面是一个您可以尝试的示例:

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; 
}

所以我使用了你的代码,并对其进行了一些修改,但我仍然遇到一个问题。除了“其他”之外,所有功能都正常运行基本上是当用户选择错误的状态时。当我选择错误的状态时,我希望正确的状态变为红色,我也希望立即转到下一个问题。我能够让它转到下一个问题,但是我只能得到“currentTarget”变成红色。关于如何初始化它以使正确的状态变成红色,有什么建议吗?我稍微调整了答案中的代码,以解决您希望发生的事情