views:

23

answers:

3
        var blockGraphics : Graphics = null;

        blockGraphics.clear();

        blockGraphics.beginFill(255);

        blockGraphics.drawRect(10,10,10,10);

I am trying to simply draw a rectangle but nothing appears on the screen. What am i missing?

A: 

I don't really know a whole lot about the graphics class (I've used it a few times) but I don't believe you can call ANYTHING on a null object.

blockGraphics = null;
blockGraphics.clear();

is the same as calling:

null.clear();

Which is going to give you an error. Typically you'll want to take a movieclip or other such object and get it's graphics instance:

blockGraphics = mc.graphics;
blockGraphics.clear();
blockGraphics.beginFill(0xFF0000);
blockGraphics.drawRect(10,10,10,10);

would draw a red rectangle inside the "mc" movieclip.

Austin Fitzpatrick
how about if i have no movie clip mc but only a stage
NativeCoder
A: 

Afaik you can't instantiate graphics class.. you need to make a MovieClip or Sprite and work with that.. you can't draw directly to stage.

var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(0xFF0000);
mc.graphics.drawRect(10,10,10,10);
mc.graphics.endFill();

also don't forget to add it to stage

addChild(mc);
Antriel
A: 
var mySprite:Sprite = new Sprite();

mySprite.graphics.beginFill(0x000000);
mySprite.graphics.drawRect(10, 10, 10, 10);
mySprite.graphics.endFill();

addChild(mySprite);
Jk_
do i have to stick with sprite, can't i directly draw on stage?
NativeCoder
What do you mean by that? What do you want to achieve?
Jk_