AS3 - 更改有关MC的深度,另一 [英] AS3 - Changing depth of MC in relation to another

查看:163
本文介绍了AS3 - 更改有关MC的深度,另一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用稍微3D精灵这种自上而下的游​​戏。我希望能够让玩家走在前面,以及背后,任何对象他来。

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.

任何想法?

推荐答案

每次玩家的动作,你需要评估每个应用项目的y位置和排序Z顺序/ childIndex基于关闭的。

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;
}

这篇关于AS3 - 更改有关MC的深度,另一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆