尽管在网络和youtube上都在搜索解决方案,但在AS3中仍然很难找到关于colorTransforming多个电影剪辑的信息。我在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;
}发布于 2015-06-26 15:26:47
将所有movieclips推入数组,然后将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;
}https://stackoverflow.com/questions/31066987
复制相似问题