Search code examples
actionscript-3stagemc

AS3 drag mc but inside another mc or other object and cut when goes out


What is easiest way to do this:

on stage 400x400 I have rect 200x200, inside rect are few mc objects. I can drag & drop StartDrag and add 200x200 as limits for this movement, but how I can do that when drag obejct they can be "visible" just near the border of rect, in other words if I drag circle into 200x200 rectangle how to make "disappear" part of that circle when it touch the border of 200x200 rect?


Solution

  • You need to add a mask to the circle. Here would be an example for the above scenario:

    var squareBG:Shape = new Shape();
    squareBG.graphics.beginFill(0);
    squareBG.graphics.drawRect(0,0,200,200);
    squareBG.graphics.endFill();
    addChild(squareBG);
    
    var circle:Sprite = new Sprite();
    circle.graphics.beginFill(0xFF0000);
    circle.graphics.drawCircle(0,0,100);
    circle.graphics.endFill();
    circle.y = 125;
    addChild(circle);
    
    var circle2:Sprite = new Sprite();
    circle2.graphics.beginFill(0xFFFF00);
    circle2.graphics.drawCircle(0,0,100);
    circle2.graphics.endFill();
    addChild(circle2);
    circle2.x = 150;
    
    var myMask:Shape = new Shape();
    myMask.graphics.copyFrom(squareBG.graphics);    
    addChild(myMask);
    
    var myMask2:Shape = new Shape();
    myMask2.graphics.copyFrom(squareBG.graphics);    
    addChild(myMask2);
    
    circle.mask = myMask;
    circle2.mask = myMask2;