从数组生成平铺图 [英] Generate Tile Map from array

查看:94
本文介绍了从数组生成平铺图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑创建一个RPG游戏,一个简单的游戏,运动,拾起物品,打开门。

I've been thinking about trying to create a RPG game, a simple game with movement, pick up items, and open doors.

我一直在想很长一段时间关于瓦片地图引擎,但我找不到任何有用的东西:/

I've been thinking for a long time about the tile map engine, but i can't find anything that works :/

基本上我想要完成的是我有一个 enum ,如:

Basically what i'm trying to accomplish is i have an enum, like:

public enum tileSort { Dirt, Grass, Stone, Empty }

当引擎运行数组时,将有0的,1等等,我在想一个switch语句,如:

And when the engine runs through the array, which will have 0's, 1's, etc, I'm thinking about a switch statement, something like:

switch(tileSort) 
{ 
    case '0':  tileList.Add(Content.Load<Texture2D>("Tiles/grass")) 
}

问题是我不知道如何使这个成为可能,所有我能够创建的是一个贯穿并产生的引擎取决于wh

The problem is that I have no idea on how to make this possible, all that I've been able to create is an engine that runs through and generates depending on which content you load first into the game.

我知道这是令人困惑的,因为我不善于解释自己。

I know this is confusing, as I'm not good at explaining myself.

提前感谢

推荐答案

您可以使用一些工具来帮助您:

You can use some tools to help you:

http://github.com/zachmu/tiled-xna

http://xnafantasy.wordpress.com/2008/11/22/ xna-map-editor-version-30-released /

我相信你可以找到许多其他人。

I'm sure you can find many others.

关于你写的代码片段,你不想打电话

About the snippets of code you wrote, you don't want to call

Content.Load<Texture2D>("Tiles/grass")

一次唱歌le纹理。加载每个纹理一次,多次打印相同的资源。你可以这样做:

multiple times for a single texture. Load each texture only once and print the same resource multiple times. You could have something like this:

var tileList = new List<Texture2D>();
string[] tiles = { "dirt", "grass", "stone", "empty" };

foreach (var s in tiles) {
    tileList.Add(Content.Load<Texture2D>("Tiles/" + s));
}

现在,每个纹理只加载一次,您可以使用 tileList [index]。

Now each texture is loaded only once, and you can access it using tileList[index].

下一步是在屏幕上打印图块。我会假设你已经将你的瓷砖加载到一个二维数组中,其中包含tile索引。

The next step is to print the tiles on the screen. I'll assume you have loaded your tiles into a 2 dimensional array with the tile indexes.

int[,] tileMap = {{1, 2, 0}, {0, 1, 2}, {3, 3, 1},};

for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        spriteBatch.Draw(tileList[tileMap[i, j]], new Vector2D(50*i, 50*j), Color.White);
        // Assuming the tiles are 50x50 pixels

本教程教你想要什么更多详细信息: http://www.xnaresources.com/?page=Tutorial:TileEngineSeries :1

This tutorial teaches what you want with more details: http://www.xnaresources.com/?page=Tutorial:TileEngineSeries:1

这篇关于从数组生成平铺图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆