So I wrote this small utility, which I will be expanding, just to save some time, but it dosent seem to draw the shape, even though there are no compiler issues?
Custom class:
package ezd.easydraw
{
import flash.display.MovieClip;
import flash.display.Shape;
public class EasyDraw extends MovieClip
{
public var _root:MovieClip = new MovieClip();
public function DrawCircle(xpos:Number=200.0, ypos:Number=200.0, r:int=50, color:uint=0x00FF00, alph:Number=1.0)
{
_root = MovieClip(_root);
var circle:Shape = new Shape;
circle.graphics.beginFill(color, alph);
circle.graphics.drawCircle(xpos, ypos, r);
circle.graphics.endFill();
_root.addChild(circle);
}
public function DrawRect(xpos:Number=200.0, ypos:Number=200.0, w:int=50, h:int=50, color:uint=0x00FF00, alph:Number=1.0)
{
_root = MovieClip(_root);
var rect:Shape = new Shape;
rect.graphics.beginFill(color, alph);
rect.graphics.drawRect(xpos, ypos, w, h);
rect.graphics.endFill();
_root.addChild(rect);
}
}
}
And the "main" window to draw the shapes:
import ezd.easydraw.EasyDraw;
stop();
var ezd:EasyDraw = new EasyDraw();
stage.focus = ezd._root;
ezd.DrawCircle(300, 300, 500);
What am I doing wrong?
What am I doing wrong?
_root
That _root
variable is totally useless. You probably copied that from some online tutorial. It's a bad practice and leads to worse practices. It recreates functionality that's already there. Do not use it.
Next up, EasyDraw
class itself. That class is basically an empty container with methods to add shapes to it. Given the general structure of the display list of adding DisplayObject
s to DisplayObjectContainer
s, this seems to be unnecessarily coupled. Why would the container bring the methods to create the children? Instead create separate classes for circles and rectangles in an object oriented manner. Then just add them to a Sprite
object if you need a container. There's no need for EasyDraw
.
The outlined concept could lead to two classes like those below. They are based on your code. Pretty naive object oriented thinking here: You want a circle? Create a class for that! You want a rectangle? Create a class for that! But it gets the job done.
package ezd.easydraw
{
import flash.display.Shape;
public class CircleShape extends Shape
{
public function DrawCircle(r:Number=50, color:uint = 0)
{
graphics.beginFill(color, 1);
graphics.drawCircle(0, 0, r);
graphics.endFill();
}
}
}
package ezd.easydraw
{
import flash.display.Shape;
public class RectangleShape extends Shape
{
public function RectangleShape(w:Number = 50, h:Number = 50, color:uint = 0)
{
graphics.beginFill(color, 1);
graphics.drawRect(-w/2, -h/2, w, h);
graphics.endFill();
}
}
}
I made some adjustments which are mostly personal preference, feel free to do this your way:
I removed the parameters that define the position from the constructor as they just bloat the constructor and every DisplayObject
defines setters for those anyway. If you insist on shoving all related values into one huge function call, I'd suggest creating a static
method that returns the created object.
That default value for color was rather odd. I changed it.
the origin is in the center of each shape
Anyway, you use them just like any DisplayObject
: create an instance and add it to a DisplayObjectContainer
. That's the important point. They blend in with the api that's already in place. Your time line code could now look something like that:
import ezd.easydraw.EasyDraw;
stop();
var circle:Circle = new Circle(300, 0xff00);
circle.x = circle.y = 300;
addChild(circle);
Now if you want to add the circle to a container, a Sprite
will do just fine:
import ezd.easydraw.EasyDraw;
stop();
var container:Sprite = new Sprite();
addChild(container);
var circle:Circle = new Circle(300, 0xff00);
circle.x = circle.y = 300;
container.addChild(circle);