如何连续为每个动画片段着色

时间:2014-03-24 20:30:19

标签: actionscript-3

我会帮助你。我有8个动画片段(square1-8)和一个名为plus1的动画片段,用于处理8个动画片段。我想点击plus1一次并着色square1,第二次按plus1并按color2等,依此类推。这是我的代码,但似乎有一些错误,因为它颜色随机。你能帮我找一下它有什么问题吗?

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


plus1.buttonMode=true;
var sximata:MovieClip = square1;
var myColorTransform:ColorTransform = new ColorTransform();


plus1.addEventListener(MouseEvent.CLICK, changeColour);

function changeColour(event:MouseEvent):void
{
if (contains(square1))
{
    removeEventListener(MouseEvent.CLICK, onsquare1);
    addEventListener(MouseEvent.CLICK, onsquare16);
        }
if (contains(square2))
{
    removeEventListener(MouseEvent.CLICK, onsquare2);
    addEventListener(MouseEvent.CLICK, onsquare3);
}
if (contains(square3))
{
    removeEventListener(MouseEvent.CLICK, onsquare3);
    addEventListener(MouseEvent.CLICK, onsquare4);
}
if (contains(square4))
{
    removeEventListener(MouseEvent.CLICK, onsquare4);
    addEventListener(MouseEvent.CLICK, onsquare5);
}
if (contains(square5))
{
    removeEventListener(MouseEvent.CLICK, onsquare5);
    addEventListener(MouseEvent.CLICK, onsquare6);
}
if (contains(square6))
{
    removeEventListener(MouseEvent.CLICK, onsquare6);
    addEventListener(MouseEvent.CLICK, onsquare7);
}
if (contains(square7))
{
    removeEventListener(MouseEvent.CLICK, onsquare7);
    addEventListener(MouseEvent.CLICK, onsquare8);
}

myColorTransform.color = 0xBDB522;

      sximata.transform.colorTransform = myColorTransform;

}



square1.addEventListener(MouseEvent.CLICK, onsquare1);
function onsquare1(e:MouseEvent):void {
sximata = square1;
}


square2.addEventListener(MouseEvent.CLICK, onsquare2);
function onsquare2(e:MouseEvent):void {
sximata = square2;
}

square3.addEventListener(MouseEvent.CLICK, onsquare3);
function onsquare3(e:MouseEvent):void {
sximata = square3;
}


 square4.addEventListener(MouseEvent.CLICK, onsquare4);
 function onsquare4(e:MouseEvent):void {
sximata = square4;
}


  square5.addEventListener(MouseEvent.CLICK, onsquare5);
  function onsquare5(e:MouseEvent):void {
sximata = square5;
}

 square6.addEventListener(MouseEvent.CLICK, onsquare6);
function onsquare6(e:MouseEvent):void {
sximata = square6;
}

square7.addEventListener(MouseEvent.CLICK, onsquare7);
function onsquare7(e:MouseEvent):void {
sximata = square7;
}


square8.addEventListener(MouseEvent.CLICK, onsquare8);
function onsquare8(e:MouseEvent):void {
sximata = square8;
}

行。我的代码现在看起来像这样

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 < squares.length - 1) {
        nextSquare = squares[index - 1];
    } else {
        trace('we are done');
      }   
   }

但是我得到了这个错误

TypeError:错误#1009:无法访问空对象引用的属性或方法。     在_fla :: MainTimeline / changeColour()

1 个答案:

答案 0 :(得分:0)

不确定为什么需要这么多事件处理程序。难道你不能把方块保持在一个数组中,每次点击plus1颜色并移动到下一个?看看下面的代码。

plus1.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 = 0xBDB522;
    nextSquare.transform.colorTransform = myColorTransform;

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