Here is an Example of How you can use the Flash Player 10 API to make a 3D cube in Flex.
If someone knows how to solidify the colors so it does not look see thru let me know.
?xml version="1.0" encoding="utf-8"?>
= new Vector.(6);
private function onCreationComplete():void
{
addChild(myCubeHolder);
myCubeHolder.x = myCubeHolder.y=250;
createCubeFaces();
myCubeHolder.addEventListener(Event.ENTER_FRAME,onCubeHolderFrame);
}
private function onCubeHolderFrame(event:Event):void
{
myCubeHolder.rotationX+=1;
myCubeHolder.rotationY+=1;
}
/**
* @Private
* We use this function to create the faces of the cube
* which will be 6
*/
private function createCubeFaces():void
{
for(var i:int=0;i<6;i++)
{
cubeFaces[i] = new FlexShape();
cubeFaces[i].graphics.beginFill(Math.random()*0xFFFFFF,1);
cubeFaces[i].graphics.drawRect(-100,-100,100,100);
cubeFaces[i].graphics.endFill();
cubeFaces[i].blendMode = BlendMode.DARKEN;
myCubeHolder.addChild(cubeFaces[i]);
}
cubeFaces[0].z=100;
cubeFaces[1].rotationY=90;
cubeFaces[2].rotationX=-90;
cubeFaces[3].rotationY=90;
cubeFaces[3].x=-100;
cubeFaces[4].rotationX=90;
cubeFaces[4].y=-100;
cubeFaces[4].z=100;
//we do nothing with cubeFace[5] since it is already
//in its place
}
]]>

6 Responses for "Simple 3D Cube in Flex using Flash Player 10."
the transparency is probably caused by how your setting your color…
Math.random()*0xFFFFFF
this is a uint so it will include a 4th byte for alpha, you can use a bit shift to remove the 4th byte before you use it as a color, then the alpha property in the beginFill method will take effect.
“If someone knows how to solidify the colors so it does not look see thru let me know.”
The colours are solid. The problem here is that the depth of the FlexShapes still apply. What you are seeing is the squares at the back of the cube being drawn on top of the squares at the front because the ones at the back have a higher depth.
Flash 10 does not support “true 3D”. You still need to take care of little things like these.
Hmmm, I am tempted to try this.
Good blog
Nice blog btw
Have you tried to change BlendMode.DARKEN to BlendMode.NORMAL?
Leave a reply