Using Vector class and drawTriangles method of Flash CS4
Posted in Flash by Flash CS3 and Flash CS4 tutorials on the 10-10-2009Using Vector Graphics and classes of Actionscript 3.0, we can draw triangles.
ActionScript 3.0 in Flash CS4 (so you need the Flash Player 10) provides the method drawTriangles.
For those unfamiliar with the Vector class in Actionscript 3.0, available for Flash Player 10, is a data structure similar to that of the array.
Implements the same methods of the Array class, but with a much stricter set of rules.
The most obvious is that the Vector class requires a BaseType designated when creating an instance and that instance will only accept items that have the same BaseType specified.
This means for example that if we declare
var sprites:Vector.<Sprite>=new Vector.<Sprite>;
In this Vector instance we can only push objects of type Sprite, unlike an array that can accept all types of objects / data.
The great benefit of this class is that each type of object that is inserted into a vector is known and we must not make a cast to assign that object to another variable as we would have to do with an Array.
Example
var sprites:Vector.<Sprite>=new Vector.<Sprite>;
var sprite:Sprite=sprites.pop();
If we try to do the same thing with an Array, Flash would return an exception error because we have not done a casting of the element removed from the Array to the Sprite.
This cleans the code and enables significantly faster execution.
That said, let’s see how to use the method drawTriangles ().
var clip_mc:MovieClip=new MovieClip();
var vertices:Vector.<Number>=new Vector.<Number>();
vertices.push(300,150);
vertices.push(450,300);
vertices.push(150,300);
clip_mc.graphics.beginFill(0x000000,1);
clip_mc.graphics.drawTriangles(vertices);
clip_mc.graphics.endFill();
addChild(clip_mc);
or
var clip_mc:MovieClip=new MovieClip();
var vertices:Vector.<Number>=new Vector.<Number>();
var ratio:int=50;
vertices.push(ratio*6,ratio*3);
vertices.push(ratio*9,ratio*6);
vertices.push(ratio*3,ratio*6);
clip_mc.graphics.beginFill(0x000000,1);
clip_mc.graphics.drawTriangles(vertices);
clip_mc.graphics.endFill();
addChild(clip_mc);
and we get the following result

Related posts
- Z axis Flash CS4 example 2 (0)
- Flash CS4 rotationX and rotationY (0)
- Brief Introduction to Flash CS4 (0)

Responses to “Using Vector class and drawTriangles method of Flash CS4”