mardi 30 juin 2015

creating duplicate movieclips overwrites their references AS3

I am trying to create 5 movieclips and add to stage. Each movieclip has a textfield attached to it that shows the reference of the movieclip (i,e., clip1,clip2, etc)

I know how to use an array to store the references to the movieclips, but what I want to do is when I clicked on clip2, for example, I want an event specific to clip2. That does not work, as the last movieclip overwrites the referneces to the previousl duplicated clips.

Here is the very simple code

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

And this is the drawLabel class

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

One more note, I could just copy the function that creates the movieclips into the main fla, and it works. But I thought why add more code when I can use the class. I hope this makes sense.

Thank you

Aucun commentaire:

Enregistrer un commentaire