我正在开发一个使用小图块生成关卡的 Flash (AS3) 小游戏。
瓷砖的位置、宽度、高度和其他属性在生成时存储在一个数组中。每个图块都添加到同一个容器影片剪辑中。当所有切片都生成后,将容器动画剪辑转换为位图,从舞台上移除所有切片,并将原始容器从舞台上移除然后删除。除了播放器之外,位图是舞台上唯一剩下的“图形”。
这比允许在游戏过程中单独渲染所有 60,000 多个图块产生更好的性能;但是,随着图块数量的增加,帧速率仍然会降低。这是没有意义的,因为所有图块都添加到一个始终相同大小的位图中,而与图块的数量无关。
生成时有 20,000 个图块,游戏以全 FPS 运行,但在 60,000 个时,它运行相对不稳定,可能在 4-5 FPS 左右。
我已经删除了碰撞检测和通过 tile 数组运行的任何/每个脚本,以排除脚本的其他一些 CPU 密集型部分落后于帧速率的可能性。
知道为什么,尽管所有图块都已从舞台上移除并且它们的容器已删除,但即使背景位图包含相同数量的数据,而不管生成的图块数量如何,游戏仍然运行缓慢?
这是关卡生成算法的最后一部分,它将容器影片剪辑转换为位图,然后删除所有图块:
var temp_bitmap_data:BitmapData = new BitmapData(this.stage_background_mc.width,this.stage_background_mc.height);
temp_bitmap_data.draw(this.stage_background_mc);
this.stage_background_bitmap = new Bitmap(temp_bitmap_data);
main_class.main.stage.addChild(this.stage_background_bitmap);
for (var block in blocks_array)
{
//every block in blocks_array has a child that is the actual graphic of the tile
blocks_array[block]["block"].removeChild(blocks_array[block]["block"].getChildAt(0));
if (blocks_array[block]["type"] == "bg_block")
{
this.stage_background_mc.removeChild(blocks_array[block]["block"]);
blocks_array[block]["block"] = null;
}
if (blocks_array[block]["type"] == "path_block")
{
//path_blocks have a second child in addition to the first one that's already been removed. the second child is the only other child
blocks_array[block]["block"].removeChild(blocks_array[block]["block"].getChildAt(0));
this.stage_background_mc.removeChild(blocks_array[block]["block"]);
}
}
this.stage_background_mc = null;
[编辑] 这是一张游戏图片,让您更好地了解正在发生的事情:
[更新:]
即使从舞台上删除最终创建的位图,最终只有 1 个孩子在舞台上,并且将删除的位图设置为 null 也不会提高速度。