一种更有效的方法来挑选和放置瓷砖?

时间:2014-04-17 06:46:09

标签: actionscript-3

像这样挑选和放置我的瓷砖是不对的? 换句话说,最好的方法是放置瓷砖,以便移除和添加不同的瓷砖很容易。或者用我设置它的方式这样做的好方法是什么。 什么是在地图上引用特定图块的好方法?

public class Stage extends MovieClip
{

    protected var tilesInWorld:Vector.<MovieClip> = new Vector.<MovieClip>();
    public var worldTiles:Sprite;
    protected var tile:MovieClip;

    protected var TILE_SIZE:int = 5;
    protected var map_width:int = 800;
    protected var map_height:int = 600;
    protected var pmap:BitmapData = new BitmapData(map_width,map_height);
    protected var _seed:uint = Math.round(Math.random() * 100);
    protected var grid_width:uint = new uint(map_width / TILE_SIZE);
    protected var grid_height:uint = new uint(map_height / TILE_SIZE);
    protected var heightmap:Array = new Array();
    protected var pixelPoint:Point = new Point();
    protected var darkest_pixel:Number = 1;
    protected var brightest_pixel:Number = 0;
    protected var hm :Number;


    public function Stage()
    {
        pmap.perlinNoise(map_width,map_height, 6, _seed, true, false, 1, true);
        for (var i:uint=0; i < grid_width; i++)
        {
            heightmap[i] = new Array();
            for (var j:uint=0; j < grid_height; j++)
            {
                heightmap[i][j] = new uint();
            }
        }
        //Divide the map in to a 7x7 grid and take data at each interval
        for (i=0; i < grid_width; i++)
        {
            for (j=0; j < grid_height; j++)
            {
                pixelPoint.x = Math.round((i/grid_width) * pmap.width)+1;
                pixelPoint.y = Math.round((j/grid_width) * pmap.height)+1;
                heightmap[i][j] = pmap.getPixel(pixelPoint.x,pixelPoint.y);
                heightmap[i][j] /=  0xffffff;

                if (heightmap[i][j] < darkest_pixel)
                {
                    darkest_pixel = heightmap[i][j];
                }
            }
        }
        //Adjust values to a min of 0
        for (i=0; i < grid_width; i++)
        {
            for (j=0; j < grid_height; j++)
            {
                heightmap[i][j] -=  darkest_pixel;

                if (heightmap[i][j] > brightest_pixel)
                {
                    brightest_pixel = heightmap[i][j];
                }
            }
        }

        //Adjust values to highest value of 1
        for (i=0; i < grid_width; i++)
        {
            for (j=0; j < grid_height; j++)
            {
                heightmap[i][j] /=  brightest_pixel;
            }
        }
        worldTiles = new Sprite();
        addChild(worldTiles);
        placeTile();
    }

下面的部分是我所指的主要部分

    protected function placeTile()
    {
        for (var i=0; i < grid_width; i++)
        {
            for (var j=0; j < grid_height; j++)
            {
                hm = heightmap[i][j]; 
                if (hm >= 0.84)
                {
                    tile = new Water();
                }
                else if (hm >= 0.8 && hm < 0.84)
                {
                    tile = new Shallow();
                }
                else if (hm >= 0.7 && hm < 0.8)
                {
                    tile = new Sand();
                }
                else if (hm >= 0.2 && hm < 0.7)
                {
                    tile = new Tile();
                }
                else
                {
                    tile = new Stone();
                }
                tile.width = TILE_SIZE;
                tile.height = TILE_SIZE;
                worldTiles.x = 0;
                worldTiles.y = 0;
                tile.x = TILE_SIZE * (i % grid_width);
                tile.y = TILE_SIZE * (j % grid_height);
                tilesInWorld.push(tile);
                worldTiles.addChild(tile);
            }
        }
    }
}

是否有更有效的方法来挑选和放置瓷砖? 虽然我引起了你的注意,但我怎么能去删除和更换瓷砖?

1 个答案:

答案 0 :(得分:3)

最好不要进行如此多的阵列查找。

 //..
 var hm :Number;
 for (var i=0; i < grid_width; i++)
 {
    for (var j=0; j < grid_height; j++)
    {
        hm = heightmap[i][j]; 

        if (hm >= 0.84)
        {
            tile = new Water();
        }
        else if ( hm >= 0.8 && hm < 0.84)
        {
             //...

您也可以查看对象池。 Here is a nice video tutorial about object pooling.

除此之外,我无法在这里看到更多优化。但我相信其他人会给你更多提示。