A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: draw closed object

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    151

    draw closed object

    can someone point me to a sample code to draw a closed object on the canvas.
    what i want is to be able to draw on the stage using mouse and only if the object is closed, then it should fill some color, else the object draw should disappear.
    what i want is very much similar to what is done in the gravity master game.
    http://www.gravitymaster.net/gravity-master-v1/

    please help.
    The One Who Would Never Die

  2. #2
    Senior Member
    Join Date
    Dec 2001
    Posts
    151
    can anyone help me on this, please, its urgent
    The One Who Would Never Die

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Maybe this is something you can adapt...
    Code:
    package {
            import flash.display.*;
            import flash.events.*;
            import flash.utils.*;
            import flash.geom.*;
    
            public class Draw extends Sprite {
                    private var obj:Sprite;
                    private var _posX:Number;
                    private var _posY:Number;
                    private var _timer:Timer = new Timer(50,0);
    
                    public function Draw() {
                            stage.addEventListener(MouseEvent.MOUSE_DOWN, begDraw);
                            stage.addEventListener(MouseEvent.MOUSE_UP, doneDraw);
                    }
                    private function begDraw($e:Event):void {
                            obj = new Sprite();
                            addChild(obj);
                            obj.graphics.endFill();
                            obj.graphics.moveTo(mouseX, mouseY);
                            obj.graphics.lineStyle(3,0x000000,1);
                            _timer.addEventListener(TimerEvent.TIMER, doDraw);
                            _timer.start();
                    }
                    private function doDraw($e:Event):void {
                            obj.graphics.lineTo(mouseX,mouseY);
                    }
                    private function doneDraw($e:Event):void {
                            var color:uint = 0xFFFF0000;
                            obj.graphics.endFill();
                            _timer.stop();
                            _timer.removeEventListener(TimerEvent.TIMER, doDraw);
                            var bmd:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,color);
                            var bmd1:BitmapData = bmd.clone();
                            var bitmap1:Bitmap = new Bitmap(bmd);
                            bmd.draw(obj);
                            bmd.floodFill(0, 0, 0x00FFFFFF);
                            var colorBounds:Rectangle = bmd.getColorBoundsRect(0xFFFFFFFF,color,true);
                            if (colorBounds.height == 0 || colorBounds.width == 0) {
                                    removeChild(obj);
                            } else {
                                    bitmap1 = new Bitmap(bmd1);
                                    bmd1.draw(obj);
                                    bmd1.floodFill(0, 0, 0x00FFFFFF);
                                    addChild(bitmap1);
                            }
                    }
            }
    }

  4. #4
    Senior Member
    Join Date
    Dec 2001
    Posts
    151
    thanks a lot dawsonk, it exactly the way i wanted it. have been trying to search but was not able to find anything useful, this is gr8.
    i am not an expert in as3, so finding it very difficult to work on something like this, this would be very helpful.

    one more question, if you could help me on that aswell.
    instead of the solid color can i fill the object with some pattern, like cross/diagonal lines.

    once again thanks for the help
    The One Who Would Never Die

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center