3 Replies Latest reply on Apr 20, 2009 10:02 AM by wvxvw

    Problem in drawing in sprite



      I draw a line on a sprite object with the moveTo and lineTo. The line is drawn over or under other elements in the application.

      How do I make the sprite or label keep the line contained within itself?



      this is my exemple,




          import flash.display.Sprite;
          import flash.events.MouseEvent;
          import mx.controls.Label;
          import mx.core.UIComponent;
          import mx.managers.CursorManager;
          public class SketchNoteClass extends Label
              private var sprite:Sprite = new Sprite();
              private var draw:Boolean = false;

              private var color:uint = 0x000000;      

              private var size:uint = 80;



              public function SketchNote()


              private function init():void
                  var comp:UIComponent = new UIComponent();
                  sprite.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
                  sprite.addEventListener(MouseEvent.MOUSE_MOVE, draw);
                  sprite.addEventListener(MouseEvent.MOUSE_UP, stop);
                  sprite.addEventListener(MouseEvent.MOUSE_OUT, stop);


              private function startDraw(evtDown:MouseEvent):void
                  sprite.graphics.lineStyle(size, color);                      
                  sprite.graphics.moveTo(sprite.mouseX, sprite.mouseY);
                  draw= true;
              private function draw(evtMove:MouseEvent):void
                  if (draw){
                      sprite.graphics.lineTo(sprite.mouseX, sprite.mouseY);
              private function stop(evtUp:MouseEvent)
                  draw= false;           





        • 1. Re: Problem in drawing in sprite
          wvxvw Level 1

          Well, there're couple of "strange" things you do...

          1. You cannot set functions to be true or false, so, whenever you do:

          draw = true or draw = false it wouldn't work (not even sure what have you tried to achieve by doing that).

          2. I don't really understand why do you need to create some internal Sprite, UIComponent etc inside something that already extends Sprite (you could simply access Label's graphics property to draw on it...

          3. are you intending to have line's size more than 0xFFFFFF (16777215 decimal) pixels (and never flip it?) why would you type size to uint?

          4. Can you elaborate, what's "contained within itself"? The line is where you draw it... it belongs to the Graphics object of the display object containing it... once you draw it you can only clear it using Graphics#clear(). But untill you do it, it will remain the part of the display object where it is drawn.

          • 2. Re: Problem in drawing in sprite
            Level 1

            What i'm trying to do is drawing inside a label or canvas ... width= 300 and heiht= 300 and not out


            1- draw = false to stop drawing


            2- i'am agree i can access label's graphic


            3- maximum size is 80, i use int


            4- I don't understand your answer, can you give me an example.




            • 3. Re: Problem in drawing in sprite
              wvxvw Level 1

              You cannot set function type variable to boolean value... well, no other language will let you do it... if you want to have a boolean variable you have to declare it as you did for other variables and set it to true / false when it is needed. Sealed method of the class cannot be changed / deleted / reassigned within that same class, assigning boolean to sealed class method should give you an error and should not compile (if it does compile for you somehow that's a compiler bug).


              Well... I'll try a different approach... every sprite has a graphics property. This property is a reference to the instance of Graphics object this sprite internaly usues to render itself.

              This object is somewhat different from other classes in a way:

              - you cannot create new instances of such object.

              - this object has no properties (only methods that eithier populate it with some visual info, or remove all info that was previously passed to it).

              So, once you draw a line into the graphics you can either erase it together with the rest of the content of the graphics, or you can add something else to it. No controll over it's size and position of the line is possible after you draw it.