In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

envelop one object by another

Get Adobe Flash player
by denver 25 Jun 2013
/**
 * Copyright denver ( http://wonderfl.net/user/denver )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/r6kr
 */

package
{
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.TextEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.text.StyleSheet;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    
    [SWF(backgroundColor="0xCCCCCC")]
    public class FlashTest extends Sprite 
    {
        private const RED_RECT:Rectangle = new Rectangle(0, 0, 100, 150);
        private const RED_SPRITE:Sprite = new Sprite();
        
        private const PURPLE_RECT:Rectangle = new Rectangle(0, 0, 80, 40);
        private const PURPLE_SPRITE:Sprite = new Sprite();
        
        private const MARKERS:Shape = new Shape();
        
        public function FlashTest() 
        {
            addChild(RED_SPRITE);
            addChild(PURPLE_SPRITE);
            addChild(MARKERS);
            
            createControls();
            drawAndTest();
        }
        
        /**
         * Вот тут то все и происходит!
         */
        private function test():void {
            var bounds:Rectangle = RED_SPRITE.getBounds(PURPLE_SPRITE);
            
            /* Отобразим полученный прямоуг на слое с маркерами */
            var points:Object = {
                "left-top": null,
                "right-top": null,
                "right-bottom": null,
                "left-bottom": null
            };
            
            points["left-top"] = new Point(bounds.left, bounds.top);
            points["right-top"] = new Point(bounds.right, bounds.top);
            points["right-bottom"] = new Point(bounds.right, bounds.bottom);
            points["left-bottom"] = new Point(bounds.left, bounds.bottom);
            
            MARKERS.graphics.clear();
            var point:Point = null;
            for each (var p:Point in points) {
                switch (p) {
                    case points["left-top"]:
                        MARKERS.graphics.beginFill(0xffff00);
                        break;
                    default:
                        MARKERS.graphics.beginFill(0xff00ff);
                }
                
                point = PURPLE_SPRITE.localToGlobal(p);
                MARKERS.graphics.drawCircle(point.x, point.y, 10);    
                p.x = point.x;
                p.y = point.y
            }
            MARKERS.graphics.endFill();            
            
            MARKERS.graphics.lineStyle(1, 0x0000ff);
            MARKERS.graphics.moveTo(points["left-top"].x, points["left-top"].y);
            MARKERS.graphics.lineTo(points["right-top"].x, points["right-top"].y);
            MARKERS.graphics.lineTo(points["right-bottom"].x, points["right-bottom"].y);
            MARKERS.graphics.lineTo(points["left-bottom"].x, points["left-bottom"].y);
            MARKERS.graphics.lineTo(points["left-top"].x, points["left-top"].y);
            
            /*
            *  Тут нужно выставить необходимые габариты PURPLE_SPRITE
            */            
            
            /* Сцентрируем сиреневый относительно красного */
            bounds = PURPLE_SPRITE.getBounds(PURPLE_SPRITE.parent);
            PURPLE_SPRITE.x = RED_SPRITE.x + (RED_SPRITE.width - bounds.width >> 1) + (PURPLE_SPRITE.x - bounds.left);
            PURPLE_SPRITE.y = RED_SPRITE.y + (RED_SPRITE.height - bounds.height >> 1) + (PURPLE_SPRITE.y - bounds.top);
        }        
        
        private function drawAndTest():void 
        {
            RED_SPRITE.rotation = 0;
            PURPLE_SPRITE.rotation = 0;
            
            RED_SPRITE.graphics.clear();
            RED_SPRITE.graphics.beginFill(0xff0000);
            RED_SPRITE.graphics.drawRect(RED_RECT.x, RED_RECT.y, RED_RECT.width, RED_RECT.height);
            RED_SPRITE.graphics.endFill();
            RED_SPRITE.graphics.beginFill(0xffff00);
            RED_SPRITE.graphics.drawRect(RED_RECT.x, RED_RECT.y, RED_RECT.width * .1, RED_RECT.width * .1);
            RED_SPRITE.graphics.endFill();
            
            PURPLE_SPRITE.graphics.clear();
            PURPLE_SPRITE.graphics.beginFill(0xff00ff, .7);
            PURPLE_SPRITE.graphics.drawRect(PURPLE_RECT.x, PURPLE_RECT.y, PURPLE_RECT.width, PURPLE_RECT.height);
            PURPLE_SPRITE.graphics.endFill();
            PURPLE_SPRITE.graphics.beginFill(0xffff00);
            PURPLE_SPRITE.graphics.drawRect(PURPLE_RECT.x, PURPLE_RECT.y, PURPLE_RECT.width * .1, PURPLE_RECT.width * .1);
            PURPLE_SPRITE.graphics.endFill();
            
            RED_SPRITE.x = stage.stageWidth - RED_SPRITE.width >> 1;
            RED_SPRITE.y = stage.stageHeight - RED_SPRITE.height >> 1;
            
            PURPLE_SPRITE.x = stage.stageWidth - PURPLE_SPRITE.width >> 1;
            PURPLE_SPRITE.y = stage.stageHeight - PURPLE_SPRITE.height >> 1;
            
            test();
        }

        private function createControls():void 
        {
            var labelTextFormat:TextFormat = new TextFormat("_sans", 16, 0, true);
            var linksCSS:StyleSheet = new StyleSheet();
            linksCSS.parseCSS("a { text-decoration: underline; color: #0000ff; font-size: 16; font-family: Arial; font-weight: bold} a:hover {text-decoration: none}");
            
            var tf:TextField = null;
            tf = new TextField();
            tf.autoSize = "left";
            tf.mouseEnabled = false;
            tf.defaultTextFormat = labelTextFormat;
            tf.text = "Red rect (WxH):";
            addChild(tf);

            tf = new TextField();
            tf.name = "redSize";
            tf.restrict = "0-9x";
            tf.type = TextFieldType.INPUT;
            tf.border = tf.background = true;
            tf.autoSize = "left";
            tf.defaultTextFormat = labelTextFormat;
            tf.text = "" + RED_RECT.width + "x" + RED_RECT.height;
            tf.x = 160;
            addChild(tf);
            
            tf = new TextField();
            tf.autoSize = "left";
            tf.mouseEnabled = false;
            tf.defaultTextFormat = labelTextFormat;
            tf.text = "Purple rect (WxH):";
            tf.y = 40;
            addChild(tf);
            
            tf = new TextField();
            tf.name = "purpleSize";
            tf.restrict = "0-9x";
            tf.type = TextFieldType.INPUT;
            tf.border = tf.background = true;
            tf.autoSize = "left";
            tf.defaultTextFormat = labelTextFormat;
            tf.text = "" + PURPLE_RECT.width + "x" + PURPLE_RECT.height;
            tf.x = 160;
            tf.y = 40;
            addChild(tf);
            
            tf = new TextField();
            tf.autoSize = "left";
            tf.mouseEnabled = true;
            tf.selectable = false;
            tf.defaultTextFormat = labelTextFormat;
            tf.styleSheet = linksCSS;
            tf.text = "<a href='event:apply'>Apply</a>";
            tf.y = 65;
            tf.addEventListener(TextEvent.LINK, function (evt:TextEvent):void {
                var redData:Array = (getChildByName("redSize") as TextField).text.split("x");
                var purpleData:Array = (getChildByName("purpleSize") as TextField).text.split("x");
                    
                RED_RECT.width = int(redData[0]);
                RED_RECT.height = int(redData[1]);
                
                PURPLE_RECT.width = int(purpleData[0]);
                PURPLE_RECT.height = int(purpleData[1]);
                
                drawAndTest();
            });
            addChild(tf);
            
            tf = new TextField();
            tf.autoSize = "left";
            tf.defaultTextFormat = labelTextFormat;
            tf.styleSheet = linksCSS;
            tf.selectable = false;
            tf.text = "Purple rotation: <a href='event:0'>0</a>  <a href='event:90'>90</a>  <a href='event:180'>180</a>  <a href='event:-90'>-90</a>  <a href='event:45'>45</a>   <b>|</b>   <a href='event:+'>+</a>  <a href='event:-'>--</a>";
            tf.y = 100;
            tf.addEventListener(TextEvent.LINK, function (evt:TextEvent):void {
                if (evt.text == "+") {
                    PURPLE_SPRITE.rotation += 5;
                } else if (evt.text == "-") {
                    PURPLE_SPRITE.rotation -= 5;
                } else {
                    PURPLE_SPRITE.rotation = int(evt.text);
                }
                
                test();
            });
            addChild(tf);            
        }
    }

}