O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Removing Vector Content

To remove all vector drawings in an object, we use the Graphics class's instance method clear( ). For example:

var canvas:Shape = new Shape();
// Draw a line
canvas.graphics.lineStyle(3, 0x0000FF);  // Apply blue stroke
canvas.graphics.lineTo(25, 35);
addChild(canvas);

// Erase the line
canvas.graphics.clear();

When the clear( ) method is invoked, the object's line style reverts to undefined (no stroke). After calling clear( ), lineStyle( ) must be called again, or no stroke will appear on lines and shapes. Calling clear( ) also resets the drawing pen position to (0, 0). Note that clear( ) affects vector content in a single object only; if that object is a Sprite or MovieClip instance, clear( ) does not erase any vector content in its children, nor does it remove them.

The following code draws a single line with a random stroke style every 250 milliseconds. It uses clear( ) to erase the previously drawn line.

package { import flash.display.*; import flash.utils.*; import flash.events.*; public class RandomLines extends Sprite { private var s:Shape; public function RandomLines () { s = new Shape(); addChild(s); var t:Timer = new Timer(250); t.addEventListener(TimerEvent.TIMER, timerListener); t.start(); } private function timerListener (e:TimerEvent):void { s.graphics.clear(); s.graphics.lineStyle(random(1, 10), random(0, 0xFFFFFF)); s.graphics.moveTo(random(0, 550), random(0, 400)); s.graphics.lineTo(random(0, 550), random(0, 400)); } // Returns a number in the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required