ColorTransform用于多个影片剪辑

时间:2015-06-26 07:05:31

标签: actionscript-3 flash-cs6

尽管在网络和youtube上搜索解决方案,仍然很难找到有关colorTransforming AS3中多个影片剪辑的信息。我在youtube上发现了一个关于colorTransform的视频,我按照步骤为单个剪辑创建了一个功能齐全的colorTransform,但是我想将它用于多个剪辑,并且能够在鼠标单击时更改每个剪辑的颜色。

我已经包含了下面的代码,也许有人知道如何添加更多的电影剪辑。当我复制并更改mc1时。 EventListener代码为mc2,我得到一个重复的函数错误,我不知道如何修复。

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

// this here is the little movieclip where the main clip gets its color from,the clip is made up of two movieclips but can also be one movieclip
// instead of brushColor i have used myColor and instead of brush.tip i have used square.
var myColor:ColorTransform=new ColorTransform();
myColor.color=0xffffff; square.transform.colorTransform=myColor

red.addEventListener(MouseEvent.CLICK,onclick);
green.addEventListener(MouseEvent.CLICK,onclick);
blue.addEventListener(MouseEvent.CLICK,onclick);
orange.addEventListener(MouseEvent.CLICK,onclick);
yellow.addEventListener(MouseEvent.CLICK,onclick);
pink.addEventListener(MouseEvent.CLICK,onclick);

function onclick(event:MouseEvent){

    if(event.target==red)
    {myColor.color=0xff0000}

    else if(event.target==green)
    {myColor.color=0x99ff33}

    else if(event.target==blue)
    {myColor.color=0x00ccff}

    else if(event.target==orange)
    {myColor.color=0xffcc33}


    enter code here
    else if(event.target==yellow)
    {myColor.color=0xffff66}

    else if(event.target==pink)
    {myColor.color=0xff99ff}

    else
    {myColor.color=0x666666}
    square.transform.colorTransform=myColor

}


mc1.addEventListener(MouseEvent.CLICK, colorChange);
function colorChange(event:MouseEvent)
{
    mc1.transform.colorTransform=myColor;
}


// upto here the code works fine but from below i get a  duplicate fuction error which i don't know how to fix.
// the idea is to add more movie clips so i can change their colors just like i can do for mc1.

1021: DUPLICATE FUNCTION DEFINITION "ERROR"

mc2.addEventListener(MouseEvent.CLICK, colorChange);
function colorChange(event:MouseEvent)
{
    mc2.transform.colorTransform=myColor;
}

1 个答案:

答案 0 :(得分:0)

将所有动画片段推送到数组中,然后将eventlistener添加到所有这些片段中,如下所示:

var mcArray:Array=new Array();
mcArray.push(mc1,mc2);
for (var i:int=0;i<mcArray.length;i++)
{
    mcArray[i].addEventListener(MouseEvent.CLICK,colorChange);
}

只添加一个colorChange函数:

function colorChange(event:MouseEvent)
{
    e.currentTarget.transform.colorTransform=myColor;
}