You need to use lineTo in the loop, instead of moveTo.
You should also look into using Point (flash.geom.Point) for the x,y positions 
rather than Array's.

Here's a slightly modified version:

import flash.geom.Point;

var RED = 0xFF0000;

// some defined (x, y) coordinates
var nw_pt:Point = new Point(100, 100);
var ne_pt:Point = new Point(200, 100);
var se_pt:Point = new Point(200, 200);
var sw_pt:Point = new Point(100, 200);

function drawPolygon(a:Array) {
 // move to the first point in list
 var p:Point = a[0];
 var len:Number = a.length;
 this.moveTo(p.x, p.y);
 trace("moveTo point: "+p);
 this.beginFill(RED, 100);
 for (var i = 1; i<len; i++) {
  p = a[i];
  this.lineTo(p.x, p.y);
  trace("lineTo point: "+p);
 }
 // finish the polygon
 this.endFill();
}
var ne_tri:Array = [nw_pt, ne_pt, se_pt];
drawPolygon(ne_tri);

regards,
Muzak

----- Original Message ----- 
From: "ej" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Friday, April 27, 2007 1:56 AM
Subject: [Flashcoders] should be simple: MovieClip drawing API


>
> I am new to ActionScript and Flash.  I was just trying to fool around with a
> very simple example using the MovieClip programmatic drawing API.  I thought I
> understood it well enough, but darned if I can make this work...   I've seen a
> few things in AS & Flash so far that are a bit buggy, so now I'm not sure if I
> am doing something stupid or there is some sort of bug here.
>
> I have two blocks of code that draw triangles fine. But when I try to draw
> another by programmatically iterating over a list of (x, y) coordinates,
> nothing gets drawn. Do you observe the same behaviour with this code? Is there
> a logical reason why the red triangle in the middle is not drawn?
>
> The trace output shows all the data I would expect, but nothing is drawn. I
> don't get it. Help?
>
> Thanks,
> -ej
>
> // ActionScript 2.0 code:
> // Just start a new FLA file and throw this all on frame 1:
>
> // some defined colors
> var BLACK   = 0x000000;
> var RED     = 0xFF0000;
> var MAGENTA = 0xFF00FF;
> var GREEN   = 0x00FF00;
>
> // some defined (x, y) coordinates
> var nw_pt:Array  = [100, 100];
> var ne_pt:Array  = [200, 100];
> var se_pt:Array  = [200, 200];
> var sw_pt:Array  = [100, 200];
>
> // this block works: draw a black triangle on the left, pointing NE
> this.moveTo(0, 100);
> this.beginFill(BLACK, 100);
> this.lineTo(100, 100);
> this.lineTo(100, 200);
> this.lineTo(0, 100);
> this.endFill();
>
> // this works fine too: green triangle pointing SW
> this.moveTo(nw_pt[0], nw_pt[1]);
> this.beginFill(GREEN, 100);
> this.lineTo(se_pt[0], se_pt[1]);
> this.lineTo(sw_pt[0], sw_pt[1]);
> this.lineTo(nw_pt[0], nw_pt[1]);
> this.endFill();
>
>
> function drawPolygon(a:Array)
> // DESCRIPTION draw a closed polygon starting at the first coord of a
> {
> // this block works too:
> // a magenta triangle, further to the right, pointing NE
> this.moveTo(200, 100);
> this.beginFill(MAGENTA, 100);
> this.lineTo(300, 100);
> this.lineTo(300, 200);
> this.lineTo(200, 100);
> this.endFill();
>
> // DOESN'T WORK:
> // where's the red triangle opposite green triangle?
>
> // move to the first point in list
> this.moveTo(a[0][0], a[0][1]);
> trace(a[0]);
>
> // start filled polygon
> this.beginFill(RED, 100);
>
> // move to indices [1..N]
> for (var i = 1; i < a.length; i++) {
> this.moveTo(a[i][0], a[i][1]);
> trace('');
> trace('a[' + i + '] = ' + a[i]);
> trace('x, y = ' + a[i][0] + ', ' + a[i][1]);
> }
>
> // move back to starting point
> this.moveTo(a[0][0], a[0][1]);
> trace(a[0]);
>
> // finish the polygon
> this.endFill();
> }
>
> var ne_tri:Array = [nw_pt, ne_pt, se_pt];
> drawPolygon(ne_tri);
>


_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to