AS3 - Changing depth of MC in relation to another

折月煮酒 提交于 2020-01-06 07:24:08

问题


I am trying to make this top-down game that uses slightly 3D sprites. I want to be able to make the player walk in front of, as well as behind, any object he comes to.

Like, if he's a certain distance behind another object, he'll move behind it and if he's closer to the front of the other object, he'll move in front of it.

Kind of like those old arcade games like streets of rage.

Any ideas?


回答1:


everytime the player moves, you need to evaluate the y position of each applicable item and sort the z-order/childIndex based off that.

so if all your 'objects' to walk behind/infront of and your character were in a parent sprite called foreground:

function sortZ(){
    var arrayOfStuff:Vector<DisplayObject> = new Vector<DisplayObject>();

    var i:int;
    for(i=0;i<foreground.numChildren;i++){
        arrayOfStuff.push(foreground.getChildAt(i));
    }

    arrayOfStuff.sort(sortArrayByY);

    for(i=0;i<arrayOfStuff.length;i++){
        foreground.setChildIndex(arrayOfStuff[i],i);
    }
}

function sortArrayByY(valA:DisplayObject, valB:DisplayObject):int {
    if(valA.y == valB.y) return 0;
    if(valA.y > valB.y) return 1;
    return -1;
}


来源:https://stackoverflow.com/questions/11992820/as3-changing-depth-of-mc-in-relation-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!