【问题标题】:Action Script 3 Counter/Button Color Change动作脚本 3 计数器/按钮颜色更改
【发布时间】:2015-05-01 02:41:24
【问题描述】:

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

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

我的问题是保持分数,我想我可以通过这两种方式进行,但我在这两种方式中都在苦苦挣扎。 第一种方法是计数器,如果你点击正确的状态给计数器加一个点,如果你点击不正确的状态不加一个点,但错误或正确,两次点击都会带你到下一个问题。这个问题是创建一个计数器,我尝试了很多东西,但似乎无法让它工作。

另一个选项是单击正确的状态会将该状态的颜色更改为绿色,单击错误的状态会将正确状态的颜色更改为红色,两次单击仍会将您带到下一个问题。我也尝试过这种方法,但似乎无法弄清楚如何更改按钮的颜色。除了我声明的 50 个状态按钮(即 virginiabutton.addEventListener(MouseEvent.CLICK, virginiabuttonclick);

编辑

所以我拿出了柜台,我真的不再需要它了,因为颜色几乎可以工作了。所以我遇到的问题是任何问题,如果你点击正确的状态,把正确的状态变成绿色,然后转到下一个问题。如果您单击错误的状态,请将正确的状态变为红色,然后转到下一个问题。除了“错误点击”功能外,这段代码完全可以做到这一点。当我单击不正确的状态时,它只会将不正确的状态(在本例中为 CA)变为红色,而不是 VA 变为红色。我认为代码中的问题在于它说“e.currentTarget”的else函数。我希望它改为将当前正确状态更改为红色而不是当前目标。

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

}

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

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

    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”变成红色。关于如何初始化它以使其正确状态变为红色的任何建议?
    • 我稍微调整了答案中的代码以解决您希望发生的事情
    猜你喜欢
    • 2015-08-13
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 2019-09-24
    • 2022-01-15
    相关资源
    最近更新 更多