游戏中,我们常常要处理场景中Sprite的前后遮挡关系。
以下公式根据TileMap坐标来动态计算ZOrder,来确保正确的遮挡关系。
为了计算的高效率,使用了位移来代替乘法运算。
// 最小左位移
int shifting = 6;
int height = map->getMapHeight();
if (height > 512) {
// 超过2^8
shifting = 10;
}else if (height > 256) {
// 超过2^7
shifting = 9;
}else if (height > 128) {
// 超过2^6
shifting = 8;
}else if (height > 64) {
// 超过2^5
shifting = 7;
}
// 根据TileMap坐标系来确定ZOrder
_sprite->setZOrder((posTile.x + posTile.y) - (map->getMapWidth() << shifting));