Firsr of all, take a look to the render function, is commented. Then i will try to explain what it does.

///Start with the update, right now is just an enterframe this.onEnterFrame = function() { //Cosine and sin calculation from the angles var cosY:Number = Math.cos(angleY); var sinY:Number = Math.sin(angleY); var cosX:Number = Math.cos(angleX); var sinX:Number = Math.sin(angleX); var cosZ:Number = Math.cos(angleZ); var sinZ:Number = Math.sin(angleZ); //We need to clear the stage to be able to draw the new cube on every frame clear(); //This array will store the distance form the camera to each Shape distanceArray = []; //Ok, let's start with each shape, remember each one is made with 4 points. for (var j:Number = 0; j<shapes.length; j++) { //Vars to store the distance from each point in the shape, in each axis. dx = 0; dy = 0; dz = 0; //Now, we need to look inside each shape, obviously, we will find 4 points, so let's take a look to all the points, and make some calculations. for (var i:Number = 0; i<shapes[j].length; i++) { //Here, is where we apply the projections for each point coordinate x1 = cosY*(sinZ*(points[shapes[j][i]].y)+cosZ*(points[shapes[j][i]].x))-sinY*(points[shapes[j][i]].z); y1 = sinX*(cosY*(points[shapes[j][i]].z)+sinY*(sinZ*(points[shapes[j][i]].y)+cosZ*(points[shapes[j][i]].x)))+cosX*(cosZ*(points[shapes[j][i]].y)-sinZ*(points[shapes[j][i]].x)); z1 = cosX*(cosY*(points[shapes[j][i]].z)+sinY*(sinZ*(points[shapes[j][i]].y)+cosZ*(points[shapes[j][i]].x)))-sinX*(cosZ*(points[shapes[j][i]].y)-sinZ*(points[shapes[j][i]].x)); //Now we store the new point coodinates, based on the angles. points[shapes[j][i]].x = x1; points[shapes[j][i]].y = y1; points[shapes[j][i]].z = z1; //we add each point coordinate to the relative distance var dx += x1; dy += y1; dz += z1; //Next code, is the way to get the X and Y points to draw in the Stage, we take care about offsets, and distance from the camera in the Z Axis //Each point will be scaled acording the distance to the camera. We are using the _x and _y as variables to render, not for calculations points[shapes[j][i]]._x = vpX+(points[shapes[j][i]].x+xOffset)*(fl/(points[shapes[j][i]].z+fl+zOffset));//scale; points[shapes[j][i]]._y = vpY+(points[shapes[j][i]].y+yOffset)*(fl/(points[shapes[j][i]].z+fl+zOffset));//scale; } //Ok, you have checked all the points in the shape, now is time too look at the distances //This is important, cos the distance to the observer, will mean which shape is render on top. //Since we have a sum of distances in X Y and Z, we need an average //Next code is the same than dividing by four (cos the four points per shape) dx /= shapes[j].length-1; dy /= shapes[j].length-1; dz /= shapes[j].length-1; //We add an element to the distances, and is basically the distance from each point, to the camera. Note than fl is on the Z axis, and is where the camera is. //We store and object with the distance, and also a shape ID to know which shape corresponds with the distance. distanceArray.push({d:Math.round(Math.sqrt(Math.pow(dx-xOffset, 2)+Math.pow(dy-yOffset, 2)+Math.pow(fl-dz, 2))), index:j}); } //Now, we need to sort the array, to know wich shape is near and need to be render on top. So a simple Sort function. distanceArray.sortOn("d"); //Here is where the fun starts. Now is time to render each polygon lineStyle(0,0xFFFFFF,40); //for each element in the distance array (the same length than the shape array, we will render the shape. for (w=0; w<distanceArray.length; w++) { //Drawing sfuff to do the line and the fill color (this can be better :D) beginFill(0xff-(distanceArray[w].index*10),100); moveTo(points[shapes[distanceArray[w].index][0]]._x,points[shapes[distanceArray[w].index][0]]._y); //Now in each shape, we draw lines to each point to close the shape. for (var j:Number = 0; j<shapes[distanceArray[w].index].length; j++) { lineTo(points[shapes[distanceArray[w].index][j]]._x,points[shapes[distanceArray[w].index][j]]._y); } //Close the shape with the endFill endFill(); } //Capture Keys pressed to move and rotate the cube checkKeys(); //DONE! };

   That's all the code we are going to use.
   Since we are going to move the cube, we need to refresh that stage a couple of times per sec. I've used 120Fps, but you may want to use a setInterval instead.
   First, i will save the sine and cosine values for each cube angle in each axis. This is basically to avoid a lot of writing later.
   Next line "clear()" is to clear the stage, cos we will use the flash drawing api. Every update, we clear before drawing.
   The nex line is an array declaration, i will explain more on this later, but basically we want to know the distance from the observer to a each shape.
   First loop start, we will follow each shape, and we will intialize 3 vars used to store a distance from each shape to the observer, we use 3 cos we need to know the 3 axis distance first.
   Then, is time to see what is inside the shape, for us: points. Here is where the important calculation comes. We usually have a point defined in 2D in flash, but now we need to use projections to determine how to show a 3D object in a 2D space.
   For our goal, we have the math needed, and is basically matrix transformations. I have to be honest and say that all i did is to follow the formula in this link http://en.wikipedia.org/wiki/3D_projection
   By using the formula, we get x1, y1 and z1 which are our 3d projection of each point.
   Then we apply those temporal vars to our point.
   Next lines are the sum of our coordinates to our distance vars that we are going to use later.
   If you take a look to the next 2 lines, you will see is very similar to the ones at wikipedia link. We have to take care from the cube offsets and also camera position. By using the formula, we have our 2d point that represents a 3d point.
   Ok, we have all the points updated, loops ends. Now we need to know the average distance from each point to the camera, and that's what the next lines do. Calculate the average, then push it in our array.

   It seems we are done, but at the moment we can just draw squares. Question is, which is in front? Cos that, we have an array made with the distance of each shape to the observer and a shape ID.
   Now, all we need to do is to sort this array by the distance and finally draw it in the screen. When the distance is greater, we draw first, when is closer, later.
   Last loop, is the real render. Follow all the distance array, then look at the shape and draw an square between the 4 points.
  You have rendered a cube, last thing is to detect when the user press a key, and that's why we call the function we alredy made.
 
  That's all!