游戏中,我们常常要处理场景中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));
本文详细介绍了如何通过位移操作优化计算TileMap坐标系下的Sprite ZOrder,以确保游戏场景中正确的遮挡关系,提高游戏性能。
1万+

被折叠的 条评论
为什么被折叠?



