开发者

AS3: newbie, problem with addChild

开发者 https://www.devze.com 2023-02-11 09:40 出处:网络
Greetings to all. I\'m very new to AS3 (and Flash CS4) and i\'m having a problem. I have this project where a user draws dynamically her signature with the mouse on an area and then, on another frame

Greetings to all.

I'm very new to AS3 (and Flash CS4) and i'm having a problem. I have this project where a user draws dynamically her signature with the mouse on an area and then, on another frame and after the signature is drawn, he can changes the color of the signature by clicking some buttons, each with a different color.

I'm using this to capture the signature to another frame:

  1. The Button that triggers the capture event:

    //targetMC is an MC to where the signature will be copied
    
    function buttonClick(event:MouseEvent):void{
        capture(drawingBoard_mc, _targetMC);
        drawGraphics.clear();
        gotoAndStop(5);
    };
    
  2. The function

    function capture(target:DisplayObject, _target:MovieClip):void {
        var relative:DisplayObject = target.parent;
        var rect:Rectangle = target.getBounds(relative);
        var bitmapData:BitmapData = new BitmapData(rect.width + PIXEL_BUFFER * 2, rect.height + PIXEL_BUFFER * 2);
        bitmapData.draw(relative, new Matrix(1, 0, 0, 1, -rect.x + PIXEL_BUFFER, -rect.y + PIXEL_BUFFER));
    
        var byteArray:ByteArray;
        var jpgEncoder:JPGEncoder = new JPGEncoder(JPG_QUALITY_DEFAULT);
        byteArray = jpgEncoder.encode(bitmapData);
        var ldr:Loader = new Loader();
        ldr.name = "signature";
        ldr.loadBytes(byteArray);
        //target is target mc where the signature will be copied into
        _target.addChild(ldr as DisplayObject);
    }
    

It copies the signature perfectly.

The problem is on target 5 where i have 2 buttons to change the color:

//Color 1
line_bt1.buttonMode = true;
line_bt1.mouseChildren = false;
line_bt1.addEventListener(MouseEvent.MOUSE_DOWN, line_bt1Over);

//Color 2
line_bt2.buttonMode = true;
line_bt2.mouseChildren = false;
line_bt2.addEventListener(MouseEvent.MOUSE_DOWN, line_bt2Over);

function line_bt1Over(e:Event){
    var myMC:DisplayObject = DisplayObject(_targetMC.getChildByName("signature") as DisplayObject);
    changeColor(myMC, 0xCCCCCC);
    changeColor(myMC, 0xCCCCCC);
}

function line_bt2Over(e:Event){
    var myMC:DisplayObject = DisplayObject(_targetMC.getChildByName("signature") as DisplayObject);
    changeColor(myMC, 0x000000);
    changeColor(myMC, 0x000000);
}

function changeColor(object:DisplayObject, color:Number){
     var colorchange:ColorTransform = new ColorTransform();
     colorchange.color = color;
     object.transform.colorTransform = colorchange;
}

My problem is the _targetMC and the signature child all change color, and i just want the child/signature.开发者_StackOverflow中文版 :(

I'm using var myMC:DisplayObject = DisplayObject(_targetMC.getChildByName("signature") as DisplayObject); to access the signature child, but the container mc (_targetMC) also changes color... What i'm i doing wrong?

Thanks in advance.


  1. One call to changeColor each should be enough.

  2. You can reuse the object's own colorTransform:

    function changeColor(object:DisplayObject, color:Number){
        var colorchange:ColorTransform = object.transform.colorTransform;
        colorchange.color = color;
        object.transform.colorTransform = colorchange;
    }
    
  3. You can omit all your type casts to DisplayObject - all Sprites and MovieClips and Loaders are subclasses of DisplayObject, so they should work fine, wherever DisplayObjects are needed:

    var myMC:DisplayObject = _targetMC.getChildByName("signature"); 
    // getChildByName always returns a DisplayObject
    

    and

    var ldr:Loader = new Loader();
    ldr.name = "signature";
    ldr.loadBytes(byteArray);
    _target.addChild(ldr); // Loader extends DisplayObject
    
  4. Your colorChange function should work as expected. There must be something else wrong. I can't be completely sure, but I would guess your problem is either related to a mixup of all your target(_mc) objects:

    • parameter "target" of function "capture"
    • parameter "_target" of function "capture"
    • member variable "_targetMC"

    Or, it might be related to adding a new Loader named "signature" to the same _targetMC every time you call capture(), but never removing the old one: After the second click, there will be more than one clip with the name "signature", and this might lead to problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消