NativeCoder
NativeCoder

Reputation: 1063

drawRect in actionscript3

        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?

Upvotes: 0

Views: 1615

Answers (3)

Antriel
Antriel

Reputation: 379

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);

Upvotes: 2

Nypam
Nypam

Reputation: 1498

var mySprite:Sprite = new Sprite();

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

addChild(mySprite);

Upvotes: 1

Austin Fitzpatrick
Austin Fitzpatrick

Reputation: 7349

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.

Upvotes: 0

Related Questions