Marianna
Marianna

Reputation: 129

function works after the second click (as3)

I have two mocieclip on stage (plus1 which colours 8 squares and minus1 which give the original colour to the squares). If i colour some squares (not all of them) and try to give them their original colour with minus1, fucntion starts after the second click and if i try to continue to colour the rest squares fucntion again starts after the second click. I can' t understand why. Can you please help me find whats wrong? Here is my code.

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

Upvotes: 1

Views: 236

Answers (1)

Bob
Bob

Reputation: 1625

The problem is that you are working on the "next square" with your minus button, when what you really want to change is the "current square". When you reverse a color change, you want to change the color of the square you just changed, not the "nextSquare" that you are about to change.

So, I would change your code as follows:

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

Upvotes: 1

Related Questions