程序生成地形

时间:2014-04-16 05:16:42

标签: actionscript-3 flash adobe

我设法得到一个有点工作程序生成的地形。但是......由于某种原因,我的瓷砖在第二行的屏幕上放置在左边1个方格,然后在最后再添加2个瓷砖:D

我确定它与

有关
            tile.y += TILE_SIZE *(Math.floor(tilesInWorld.length/ROW_LENGTH));
            tile.x = TILE_SIZE * (tilesInWorld.length-(ROW_LENGTH*Math.floor(tilesInWorld.length/ROW_LENGTH)));

但我不确定...... 下面是生成的地形的代码

package 
{

import flash.display.*;
import flash.events.Event;

public class World
{

    protected var tilesInWorld:Vector.<MovieClip> = new Vector.<MovieClip>();
    public var worldTiles:Sprite;
    protected var tile:MovieClip;
    protected var TILE_SIZE = 25;
    protected var MAP_WIDTH = 800;
    protected var MAP_HEIGHT = 600;
    protected var ROW_LENGTH = (MAP_WIDTH/TILE_SIZE)+1;
    protected var COL_LENGTH = (MAP_HEIGHT/TILE_SIZE);
    protected var MAX_WATER = 100;
    protected var waterTiles;
    protected var nextTile:String;
    protected var maxTiles = ROW_LENGTH * COL_LENGTH;
    protected var waterChain:int;
    protected var waterBuffer:int;

    public function World(parentMC:MovieClip)
    {
        waterBuffer = Math.random()*(maxTiles-MAX_WATER);
        waterTiles = 0;
        waterChain = 5;
        nextTile = "";
        parentMC.addEventListener(Event.ENTER_FRAME, update);
        worldTiles = new Sprite();
        parentMC.addChild(worldTiles);
    }

    protected function generateTile()
    {
        if (tilesInWorld.length > 0)
        {
            if (waterTiles >= MAX_WATER)
            {
                tile = new Grass();
                nextTile = "grass";
                trace(waterTiles);
            }
            else
            {
                if(tilesInWorld.length + 1 < waterBuffer){
                    nextTile = "grass";
                }
                for (var i:int = 0; i < tilesInWorld.length; i++)
                {
                    if (tilesInWorld[i].x == (tilesInWorld[tilesInWorld.length-1].x + TILE_SIZE) && tilesInWorld[i].y == (tilesInWorld[tilesInWorld.length-1].y-TILE_SIZE) && tilesInWorld[i].type == "water")
                    {
                        nextTile = "water";
                    }
                }
                if (nextTile == "grass")
                {
                    tile = new Grass();
                    nextTile = "";
                    waterChain = 0;
                }
                else if (nextTile == "water")
                {
                    waterTiles +=  1;
                    waterChain +=  1;
                    tile = new Water();
                    if (waterChain > 5)
                    {
                        nextTile = "";
                    }
                    else
                    {
                        nextTile = "water";
                    }
                }
                else
                {
                    if (Math.random() * 1 >= 0.25)
                    {
                        waterChain = 0;
                        tile = new Grass();
                        nextTile = "grass";
                    }
                    else
                    {
                        waterTiles +=  1;
                        waterChain +=  1;
                        tile = new Water();
                        nextTile = "water";
                    }
                }
            }

        }
        else
        {
            if (Math.random() * 1 >= 0.5)
            {
                waterChain = 0;
                nextTile = "grass";
                tile = new Grass();
            }
            else
            {
                waterTiles +=  1;
                waterChain +=  1;
                nextTile = "water";
                tile = new Water();
            }
        }

这是放置瓷砖的X和Y部分的实际编码所在的位置,换句话说,这是我认为错误在哪里的地方

        tilesInWorld.push(tile);
        tile.width = TILE_SIZE;
        tile.height = TILE_SIZE;
        worldTiles.x = 0-(tile.width/2);
        worldTiles.y = 0+(tile.height/2);
        tile.x = TILE_SIZE * tilesInWorld.length;
        if (tile.x >= MAP_WIDTH)
        {
            tile.y += TILE_SIZE * (Math.floor(tilesInWorld.length/ROW_LENGTH));
            tile.x = TILE_SIZE * (tilesInWorld.length-(ROW_LENGTH*Math.floor(tilesInWorld.length/ROW_LENGTH)));
        }
        worldTiles.addChild(tile);
    }
    protected function allowTile():Boolean
    {
        //if()
        if (tilesInWorld.length > maxTiles)
        {
            return false;
        }
        return true;
    }
    protected function update(e:Event)
    {
        if (allowTile())
        {
            generateTile();
        }
    }

}

}

这是一个游戏链接,你可以真正看到它在做什么 http://www.fastswf.com/p13LrYA 刚刚意识到你无法看到它在做什么,因为它离屏幕lolol:D

另外,如果有人能帮助在地形上制作随机湖泊的更好方法吗?

但请提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

更改

protected var ROW_LENGTH = (MAP_WIDTH/TILE_SIZE); //remove the +1  

删除

if (tile.x >= MAP_WIDTH)

您可以使用以下方法计算其索引中的切片x,y(可以在添加之前使用tilesInWorld.length获得):

tile.x = TILE_SIZE * (tilesInWorld.length % ROW_LENGTH);
tile.y = TILE_SIZE * Math.floor(tilesInWorld.length / ROW_LENGTH);