简体   繁体   English

瓷砖系统和瓷砖绘图

[英]Tile System and Tile Drawing

I'm triyng to make an Automated type tile system to draw my int[,] tileMap according to my Texture Atlas but when i run the code i get the entire thing sideways and the textures don't appear, the only way i know it's sideways is because the "Color color" for drawing the textures are different then the ClearScreen's color. 我正在根据我的纹理图集制作一个Automated类型的瓷砖系统来绘制我的int [,] tileMap,但是当我运行代码时,我将整个东西侧身并且纹理没有出现,这是我知道它的唯一方法侧面是因为绘制纹理的“颜色”与ClearScreen的颜色不同。

Tile Sorting Class: 瓷砖排序类:

    public Texture2D Texture { get; set; }
    public int Rows { get; set; }
    public int Columns { get; set; }
    public int totalTiles;
    public int _tileWidth;
    public int _tileHeight;
    public int[] tiles;
    public int[,] tileMap = new int[,] {
            {0,0,0,0,0,0,2,0,0,0,},
            {0,0,0,0,0,2,2,0,0,0,},
            {0,0,0,0,0,2,0,0,0,0,},
            {0,0,0,0,0,2,0,0,0,0,},
            {0,0,0,2,2,2,0,0,0,0,},
            {0,0,0,2,0,0,0,0,0,0,},
            {0,0,0,2,0,0,0,0,0,0,},
            {0,0,2,2,0,0,0,0,0,0,},
            {0,0,2,0,0,0,0,0,0,0,},
            {0,0,2,0,0,0,0,0,0,0,}
    };

    public Tile(Texture2D texture, int tileWidth, int tileHeight)
    {
        _tileWidth = tileWidth;
        _tileHeight = tileHeight;
        Texture = texture;
        totalTiles = Rows * Columns;

    }


    public void Update() {

    }

    public void Draw(SpriteBatch spriteBatch, Vector2 location)
    {

        for (int row = 0; row < tileMap.GetLength(1); row++) {

            for (int col = 0; col < tileMap.GetLength(0); col++) {

                switch (tileMap[row, col]) {
                    case 0:
                        spriteBatch.Draw(Texture, new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight), new Rectangle(1 * _tileWidth, 1 * _tileHeight, _tileWidth, _tileHeight), Color.Transparent);
                        break;
                    case 2:
                        spriteBatch.Draw(Texture, new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight), new Rectangle(2 * _tileWidth, 2 * _tileHeight, _tileWidth, _tileHeight), Color.Transparent);
                        break;
                    default:
                        break;
                }

            }

The Main Class 主类

public class Main : Microsoft.Xna.Framework.Game {
    // Basic code for drawing.
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Tile test;
    Texture2D texturePack;

    public Main() {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize() {

        base.Initialize();
    }


    protected override void LoadContent() {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        texturePack = Content.Load<Texture2D>("AxiomTileSheet");
        test = new Tile(texturePack, 8, 8);
    }


    protected override void UnloadContent() {
    }


    protected override void Update(GameTime gameTime) {

        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();


        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime) {
        GraphicsDevice.Clear(Color.White);

        spriteBatch.Begin();
        test.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

this 这个

switch (tileMap[row, col]) 

should be 应该

switch (tileMap[col, row])

as you use col for you x values and row for y. 当你使用col为你x值和row为y。

You also need to switch this in your rectangle calculations. 您还需要在矩形计算中切换它。

Another hint for performance based on my experience (as Andrew mentioned this may not be worthwhile in every case) instead of creating new rectangles every frame define 2 rectangles in you Tile class and just change the x and y values every frame 根据我的经验提供性能的另一个提示(正如Andrew提到的那样,在每种情况下都不值得)而不是每帧都创建新的矩形在Tile类中定义2个矩形,并且每帧只更改x和y值

floAr floAr

EDIT: Color Problem============================= 编辑:颜色问题=============================

You can completly get rid of the switch case construct by using 您可以通过使用完全摆脱switch case构造

public void Draw(SpriteBatch spriteBatch)
        {
    for (int row = 0; row < tileMap.GetLength(1); row++) {
        for (int col = 0; col < tileMap.GetLength(0); col++)
            {
                        spriteBatch.Draw(_texture, new Rectangle(row * _tW, col * _tH, _tW, _tH), new Rectangle(tileMap[row, col] * _tW, tileMap[row, col] * _tH, _tW, _tH), Color.White);
            }
        }
        }

Note: _tH=_tileHeight, _tW=_tileWidth I tested this out and it worked like a charm for me ;) If you want to keep the switch case make sure you set Color.White as your tinting color (last spritebatch parameter). 注意:_tH = _tileHeight,_tW = _tileWidth我测试了它,它对我来说就像一个魅力;)如果你想保持开关盒,请确保将Color.White设置为你的着色颜色(最后一个spritebatch参数)。 And you need to correct the values of the switch case, as you use 0 and 2 in your map definition, but 0 and 1 in your switch case. 并且您需要更正开关盒的值,因为在地图定义中使用0和2,但在开关盒中使用0和1。 Expanding the cases up to 3 (as it seems you have 4 tiles by now) also worked for me. 将案例扩展到3(因为看起来你现在有4个瓷砖)也适合我。

So I think your issue was based on the wrong color and a typo in the case structure. 所以我认为你的问题是基于错误的颜色和案例结构中的拼写错误。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM