创建重复的动画片段将覆盖其引用AS3

时间:2015-06-30 17:28:10

标签: actionscript-3 flash

我正在尝试创建5个动画片段并添加到舞台。每个动画片段都附有一个文本字段,显示动画片段的引用(例如,clip1,clip2等)

我知道如何使用数组来存储对动画片段的引用,但我想要做的是当我点击clip2时,例如,我想要一个特定于clip2的事件。这不起作用,因为最后一个movieclip会覆盖以前复制剪辑的引用。

这是非常简单的代码

//code below resides on frame 1, Flash IDE
import com.Jim.util.drawLabel;
import flash.text.TextFormat;
import flash.display.MovieClip;
import flash.events.MouseEvent;

var d:drawLabel;//this is a class that creates the shapes, texts of the mcs
var f:TextFormat = new TextFormat("Arial");

for (var i:int=0; i<4; i++) {
    d = new drawLabel  ;
    d.idOf=i
    d.init("i "+i,i,f,50,50*i);//the drawlabel class has a public method that creates the shapes and the text; see below in the class
    d.addEventListener(MouseEvent.CLICK,checked);
    addChild(d);
}
function checked(e:MouseEvent):void {
    trace(d.idOf);
//result of the trace is 3, regardless of which movieclip I clicked on;
}

这是drawLabel类

package com.Jim.util{

    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.GradientType;
    import flash.geom.Matrix;
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.text.TextFormat;
    import flash.display.Sprite

    public class drawLabel extends MovieClip {


        public var bc:Sprite;
        public var idOf:Number
        public var label_txt:TextField;
        public var theSentence:String;

        public function drawLabel() {

        }
        public function init(theSentence,mc_id,whatFmt,sentenceL:Number=100,sx:Number=0,sy:Number=0,blockSize:Number=50,colorOf:uint=0xcccccc, alphaOf:int=1) {
            bc = new Sprite();
            idOf=mc_id
            label_txt = new TextField();
            bc.graphics.beginFill(colorOf,alphaOf);
            bc.graphics.drawRect(sx, sy, sentenceL, blockSize);
            bc.graphics.endFill();
            bc.mouseChildren=true
            bc.buttonMode=true
            label_txt.x = sx;
            label_txt.y = sy;
            label_txt.width = sentenceL;
            label_txt.height = blockSize;
            label_txt.multiline = true;
            label_txt.wordWrap = true;
            label_txt.border = false;
            label_txt.type = "dynamic";
            label_txt.selectable = false;
            label_txt.text = theSentence;
            label_txt.setTextFormat(whatFmt);
            addChild(bc);
            bc.addChild(label_txt);
            bc.addEventListener(MouseEvent.CLICK,clicked);
        }
    }
}

还有一点需要注意,我可以将创建动画片段的功能复制到主fla中,并且可以正常工作。但我想为什么在我可以使用该类时添加更多代码。我希望这是有道理的。

谢谢

1 个答案:

答案 0 :(得分:0)

您只需使用Event.currentTarget即可访问当前对象,因此您可以这样做:

function checked(e:MouseEvent):void {
    trace(drawLabel(e.currentTarget).idOf);
}

希望可以提供帮助。