【问题标题】:ColorTransform for multiple movie clips多个影片剪辑的 ColorTransform
【发布时间】:2015-06-26 07:05:30
【问题描述】:

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

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

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

【问题讨论】:

标签: actionscript-3 flash-cs6


【解决方案1】:

将所有的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;
}

【讨论】:

  • 非常感谢您的帮助一百万次。
  • 我尝试了上面的代码,但出现以下错误。 1120: 访问未定义的属性 e.
  • 改成这个:function colorChange(e:MouseEvent)
猜你喜欢
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 2011-01-19
相关资源
最近更新 更多