函数在第二次单击后起作用(as3)

时间:2014-03-29 19:31:07

标签: actionscript-3

我在舞台上有两个mocieclip(plus1,其中8个正方形和-1个颜色,这些正方形为原始颜色)。如果我为一些正方形(不是所有正方形)着色并尝试用minus1给它们原始颜色,则在第二次点击后启动功能,如果我尝试继续着色其余的正方形,则在第二次点击后再次启动。我不明白为什么。你能帮我找到什么错误的吗?这是我的代码。

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;


plus1.buttonMode=true;
minus1.buttonMode=true;


var nextSquare:MovieClip = square1;

var squares:Array = [square1, square2, square3, square4, square5, square6, square7, square8];

var myColorTransform:ColorTransform = new ColorTransform();


plus1.addEventListener(MouseEvent.CLICK, changeColour);

function changeColour(event:MouseEvent):void
{
    myColorTransform.color = 0x519596;
    nextSquare.transform.colorTransform = myColorTransform;

    var index = squares.indexOf(nextSquare);
    if (index < squares.length - 1) {
        nextSquare = squares[index + 1];
    } else {
        trace('we are done');
    }   
}

minus1.addEventListener(MouseEvent.CLICK, reversecolour);

function reversecolour(event:MouseEvent):void
{
    nextSquare.transform.colorTransform = new ColorTransform();

       var index = squares.indexOf(nextSquare);
    if (index >0) {
        nextSquare = squares[index - 1];
    } else {
        trace('we are done');
    }   
}

1 个答案:

答案 0 :(得分:1)

问题是你正在研究&#34;下一个广场&#34;使用你的减号按钮,当你真正想要改变的是&#34;当前正方形&#34;。当您反转颜色更改时,您想要更改刚更改的方块的颜色,而不是&#34; nextSquare&#34;你将要改变。

所以,我会按如下方式更改您的代码:

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;


plus1.buttonMode=true;
minus1.buttonMode=true;


var nextSquare:MovieClip = square1;
var currentSquare:MovieClip = null;
var squares:Array = [square1, square2, square3, square4, square5, square6, square7, square8];

var myColorTransform:ColorTransform = new ColorTransform();


plus1.addEventListener(MouseEvent.CLICK, changeColour);

function changeColour(event:MouseEvent):void
{
    myColorTransform.color = 0x519596;
    nextSquare.transform.colorTransform = myColorTransform;
    currentSquare = nextSquare;
    var index = squares.indexOf(nextSquare);
    if (index < squares.length - 1) {
        nextSquare = squares[index + 1];
    } else {
        trace('we are done');
    }   
}

minus1.addEventListener(MouseEvent.CLICK, reversecolour);

function reversecolour(event:MouseEvent):void
{
    currentSquare.transform.colorTransform = new ColorTransform();
    nextSquare =  currentSquare;
       var index = squares.indexOf(currentSquare);
    if (index >0) {
        currentSquare = squares[index - 1];
    } else if (index == -1){
         trace("First change a colour");
    } 
    else {
        trace('we are done');
    }   
}