Skip to main content
Studio PROJEKTOR
Participating Frequently
May 20, 2011
Question

3D efect problem

  • May 20, 2011
  • 1 reply
  • 567 views

Hello.

I'm new here and just start my adventure with as3.

I have a problem with a 3D effect during play a rotation of the cube.
I made a cube with sample textures and texture of the walls while playing "overlap" each other.

Source code:

function rotate(e:Event){
cube.rotationX+=0
cube.rotationY-=1
}
stage.addEventListener(Event.ENTER_FRAME,rotate)

Source files to download

http://www.fhu-projektor.pl/flash/cube.fla


Please help.

This topic has been closed for replies.

1 reply

Kenneth Kawamoto
Community Expert
Community Expert
May 20, 2011

Flash does not sort display list z-order according to the z position, so that even if something is behind in the 3D space it appears on top if it has a higher display order index. You need to sort this out manually...

Before do this you need to re construct your cube so that it consists of 6 single MovieClips (instead of collection of nested MovieClips).

import flash.display.DisplayObjectContainer;
import flash.display.DisplayObject;
import flash.events.Event;

function rotate(e:Event) {
    cube.rotationX +=  0;
    cube.rotationY -=  1;
    try {
        sortZ(cube);
    } catch (e) {
        //
    }
}

function sortZ(container:DisplayObjectContainer):void {
    var sortArray:Array = new Array();
    var i:uint;
    var childCount:uint = container.numChildren;
    for (i = 0; i < childCount; i++) {
        var child:DisplayObject = container.getChildAt(i);
        var zLoc:Number = child.transform.getRelativeMatrix3D(stage).position.z;
        sortArray.push({displayObject:child, screenZ:zLoc});
    }
    sortArray.sortOn("screenZ", Array.NUMERIC | Array.DESCENDING);
    for (i = 0; i < childCount; i++) {
        container.setChildIndex(sortArray.displayObject, i);
    }
}

stage.addEventListener(Event.ENTER_FRAME,rotate);

(The credit goes to Ralph Hauwert http://unitzeroone.com/blog/?s=zsort)

Studio PROJEKTOR
Participating Frequently
May 20, 2011

Big thanks kennethkawamoto2.

Everything works as it should!