Mx - Drawing API

I got this code for drawing polyhedrons with the drawing API from a tutorial but I don’t understand what everthing does. I get the first 5 lines but then it loses me. Can someone break it down line by line? The biggest thing I dont get is how it acceses the position in the array.

[AS]
_root.createEmptyMovieClip(“holder”,1); //creates clip
holder.lineStyle(1,0,100);//defines line style
width=30;//sets half the width of the square

myPoints=[[50-width,50-width],[50+width,50-width],[50+width,50+width],[50-width,50+width],[50-width,50-width]];
//array with the 5 points needed to define the square

holder.moveTo(this.myPoints[0][0],this.myPoints[0][1]);
//moves it to the starting point
for (var j=1;j < myPoints.length;j++)
holder.lineTo(this.myPoints[j][0],this.myPoints[j][1]);
//here i am lost
[/AS]

Which part exactly don’t you understand? The for loop or the multidimensional arrays (this.myPoints[j][0]), or both?

array. I understand exactly what the for loop does.

Ok, the myPoints array contains 5 items. But if you look carefully, those 5 items are actually arrays that contain 2 items ([] signifies an array).

So each item in the myPoints array is actually another array.

Arrays start counting from 0, so those sub arrays are basically like…
[position0, position1]

So now for the script…
[As]this.myPoints[j][0][/AS]

You know this.myPoints is the main array the contains the points so this.myPoints[j] gets the position of the item in the main array, and the [0] gets the position of the item at position0 in the subArray (array within the myPoints item array). Naturally to get the 2nd position you would change [0] to [1] (which is in the script as well).

So basically [AS]holder.lineTo(this.myPoints[j][0],this.myPoints[j][1]);[/AS]

lineTo works like this… lineTo(x,y), so basically the script gets the position in the this.myPoints[j][0] array for the x position and the position in the this.myPoints[j][1]) array for the y position and then it draws a line there.

Does that help any?

Oh yeah. See I am used to c++ where [] accesses a position in an array, not creates a new one. I thought it was trying to call 2 positions out of a 1 demensional array. I get it now. Thanks

Yeah, in Flash [] creates an array and arrayName[] accesses a position in an array. And in the case of multidimensional arrayes it would be arrayName[][] or even arrayName[][][] so on and so forth :slight_smile:

Glad to help clear things up =)

Yup. Thanks a bunch. On a totally unrelated note, does recursion work in Flash?

Yes, I believe so. I don’t really work with recursive math in Flash though so i’m not entirely sure if/how it works.

ok. :slight_smile: thanks