对象池没有正确返回对象

时间:2014-06-11 18:54:57

标签: actionscript-3 flash

我使用对象池建立了一个程序生成的游戏。第一个区域总是正常加载,有时下一个区域或2也会加载精细。然后它会中断并且我编码抛出的错误被抛出告诉我我的池中没有足够的对象,这很奇怪,考虑到它应该在尝试加载下一个区域之前返回它所使用的所有对象。

游戏的链接,看看它在做什么 http://www.fastswf.com/pu06z-A

我甚至在我的精灵池类中添加了一个countSprites函数来返回计数器然后使得generateTile()函数等待,直到池恢复到192,但它似乎仍然无效:/

在做了一堆痕迹并且如果测试之后,iv得出结论,它返回的对象很好,问题是它实际上是从地图的开始到现在的区域。

这是在屏幕上生成我的图块的功能。

    public function generateTile()
    {
        for (var i:int = X/GlobalCode.MAP_SCALE; i < (X + (800/TILE_SIZE)/GlobalCode.MAP_SCALE); i++)
        {
            for (var j:int = Y/GlobalCode.MAP_SCALE; j < Y + (600/TILE_SIZE)/GlobalCode.MAP_SCALE; j++)
            {
                hm = heightmap[i][j];
                if (hm >= 0.84)
                {
                    tile = waterPool.getSprite(); //this is where the tiles are taken from the sprite pool
                }
                else if (hm >= 0.8 && hm < 0.84)
                {
                    tile = shallowPool.getSprite();//this is where the tiles are taken from the sprite pool
                }
                else if (hm >= 0.7 && hm < 0.8)
                {
                    tile = sandPool.getSprite();//this is where the tiles are taken from the sprite pool
                }
                else if (hm >= 0.2 && hm < 0.7)
                {
                    tile = tilePool.getSprite();
                }
                else
                {
                    tile = stonePool.getSprite();//this is where the tiles are taken from the sprite pool
                }
                tile.width = TILE_SIZE;
                tile.height = TILE_SIZE;
                worldTiles.x = 0;
                worldTiles.y = 0;
                tile.x = TILE_SIZE * (i % 800);
                tile.y = TILE_SIZE * (j % 600);
                tilesInWorld.push(tile);
                worldTiles.addChild(tile);
            }
        }
    }

这是删除它们的功能。

    public function deleteTiles()
    {
        if (tilesInWorld.length > 0)
        {
            for (var i:int = tilesInWorld.length-1; i >= 0; i--)
            {
                if(tilesInWorld[i].Name == "water"){
                    waterPool.returnSprite(tilesInWorld[i]);//this is the specific line of code that is returning the objects to the pool
                    trace("water returned");
                }else if(tilesInWorld[i].Name == "shallow"){
                    shallowPool.returnSprite(tilesInWorld[i]);//this is the specific line of code that is returning the objects to the pool
                    trace("shallow returned");
                }else if(tilesInWorld[i].Name == "sand"){
                    sandPool.returnSprite(tilesInWorld[i]);//this is the specific line of code that is returning the objects to the pool
                    trace("sane returned");
                }else if(tilesInWorld[i].Name == "grass"){
                    tilePool.returnSprite(tilesInWorld[i]);
                    trace("grass returned");
                }else{
                    stonePool.returnSprite(tilesInWorld[i]);//this is the specific line of code that is returning the objects to the pool
                    trace("stone returned");
                }
                worldTiles.removeChild(tilesInWorld[i]);//this is the specific line of code that is returning the objects to the pool
            }
            tilesInWorld.length = 0;//empty array
                        if(tilePool.countSprites == 192){
            generateTile();
                        }
        }
    }

这是首次创建对象池的位置

        tilePool = new SpritePool(Tile, 192);
        sandPool = new SpritePool(Sand, 192);
        shallowPool = new SpritePool(Shallow, 192);
        waterPool = new SpritePool(Water, 192);
        stonePool = new SpritePool(Stone, 192);

不要认为有一点可以证明这一点,但只是因为这是我的SpritePool类

  package  {
    import flash.display.MovieClip;

    public class SpritePool {

    private var pool:Array;
    private var counter:int;

    public function SpritePool(type:Class, len:int) {
        pool = new Array();
        counter = len;

        var i:int = len;
        while(--i > -1){
            pool[i] = new type();
        }
    }

    public function getSprite():MovieClip
    {
        if(counter > 0){
            return pool[--counter];
        }
        else{
            throw new Error("You exhausted the pool!");
        }
    }
    public function returnSprite(s:MovieClip):void
    {
        pool[counter++] = s;
    }

    public function countSprites():int
    {
        return counter;
    }

}

}

1 个答案:

答案 0 :(得分:0)

变化

    for (var i:int = X/GlobalCode.MAP_SCALE; i < (X + (800/TILE_SIZE)/GlobalCode.MAP_SCALE); i++)
    {
        for (var j:int = Y/GlobalCode.MAP_SCALE; j < Y + (600/TILE_SIZE)/GlobalCode.MAP_SCALE; j++)
        {

var Xcalc = (X + (800/TILE_SIZE)/GlobalCode.MAP_SCALE);
var Ycalc = (Y + (600/TILE_SIZE)/GlobalCode.MAP_SCALE);

    for (var i:int = X; i < Xcalc; i++)
    {
        for (var j:int = Y; j < Ycalc; j++)
        {