How Do I Draw with Code?: Chapter 12 - ActionScript 3.0 Quick Reference Guide
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

12.2 Referencing an Object’s Graphics Property


You want to efficiently reference the graphics property of a display object, such as a sprite or the main timeline, to use as the target for vector-based drawing.


Store a reference to the graphics property in a variable.


To draw with vectors, you must first reference the graphics property of a display object. Then you can access the methods and properties of the Graphics class, which is responsible for dynamic vector drawing. You can reduce code length and improve performance by storing a reference to the property in a variable, and using that variable thereafter where you would otherwise have referenced the property directly.

If you want to draw into a display object, for example, such as the sprite sp you created in the section called “12.1 Creating a Display Object Dynamically”, you can reference its graphics property like so:

var g:Graphics =;

Thereafter, you need only refer to g when using a method or property of the Graphics class. You can also reference the graphics property of the main timeline with the following:

var g:Graphics =;

This line of code assumes you are writing the script in the main timeline, so the this keyword refers to the correct display object: the main timeline itself.


While it’s a matter of personal coding preference, drawing directly on the main timeline isn’t usually as useful as being able to contain your drawing in a display object you can manipulate later.

Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

Next Pagearrow