Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change order of sprite rendering #46

Open
mefilt opened this issue May 9, 2012 · 0 comments
Open

change order of sprite rendering #46

mefilt opened this issue May 9, 2012 · 0 comments

Comments

@mefilt
Copy link

mefilt commented May 9, 2012

i implemented change order of sprite rendering

add Node2D
public var z_order:Number = 0;
private var isReorderChildDirty:Boolean = false;

add method Node2D

        public function addChild(child:Node2D):Node2D {
            return addChildAt(child, children.length);
        }

        public function addChildZ(child:Node2D,z:Number):Node2D {
            isReorderChildDirty = true;
            return addChildAtZ(child, children.length,z);
        }


        public function addChildAtZ(child:Node2D,idx:uint,z:Number):Node2D
        {
            var existingIdx:int = getChildIndex(child);
            if(existingIdx != -1) {
                removeChildAt(existingIdx);
            }

            if(isBatchNode) {
                child.isBatchNode = isBatchNode;
            }
            child.z_order = z
            child.parent = this;
            child.setStageAndCamRef(_stage, camera);
            children.splice(idx, 0, child);

            return child;
        }

        public function addChildAt(child:Node2D, idx:uint):Node2D {

            return this.addChildAtZ(child,idx,children.length);

        }

               protected function sortAllChildren():void
        {
            //q-sort
            if (isReorderChildDirty) {
                if(children.length > 0) {
                    quickSort(children,0,children.length - 1);
                    isReorderChildDirty = false;
                }

            }
        }
        private function quickSort(vector:Vector.<Node2D>, left:Number, right:Number):void {

            var i:Number = 0,j:Number = 0;
            var node:Node2D = null;
            var tempNode:Node2D = null;

            i = left;
            j = right;
//          trace ('i<=j'+i+' <= '+ ' '+j);
            node = children[Math.round((left+right)*0.5)];
            while (i<=j) {
//              trace('vector[i].z_order '+vector[i].z_order);
//              trace('node.z_order) '+node.z_order);
                while (vector[i].z_order < node.z_order) {
                    i++;
                }
                while (vector[j].z_order > node.z_order) {
                    j--;
                }
                if (i<=j) {
                    tempNode = vector[i];
                    vector[i] = vector[j];
                    i++;
                    vector[j] = tempNode;
                    j--;
                }
            }
            if (left < j) {
                quickSort(vector, left, j);
            }
            if (i < right) {
                quickSort(vector, i, right);
            }
            return;
        }

                public function setZorder(z:Number):void
        {
            if (parent) {
                parent.reorderChild(z,this);
            }



        }


              public function reorderChild(z:Number,child:Node2D):void
        {
            if (!child) {
                return;
            }
            child.z_order = z;
            isReorderChildDirty = true;
        }

Modify method Node2D

    internal function drawNode(context:Context3D, camera:Camera2D, parentMatrixChanged:Boolean, statsObject:StatsObject):void {

        var myMatrixChanged:Boolean = false;

        if(!_visible) {
            return;
        }

        sortAllChildren();
              ...}

Modify method Scene2D

        override internal function drawNode(context:Context3D, camera:Camera2D, parentMatrixChanged:Boolean,   statsObject:StatsObject):void {


            sortAllChildren();

            for each(var child:Node2D in children) {
                child.drawNode(context, camera, false, statsObject);
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant