0

自動化されたタイプのタイル システムを作成して、テクスチャ アトラスに従って int[,] tileMap を描画しようとしていますが、コードを実行すると全体が横向きになり、テクスチャが表示されません。横にあるのは、テクスチャを描画するための「色の色」が ClearScreen の色と異なるためです。

タイル分類クラス:

    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;
                }

            }

メインクラス

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);
    }
}
4

1 に答える 1

2

これ

switch (tileMap[row, col]) 

する必要があります

switch (tileMap[col, row])

x 値に col を使用し、y に row を使用するためです。

また、長方形の計算でこれを切り替える必要があります。

フレームごとに新しい長方形を作成する代わりに、私の経験に基づいたパフォーマンスのもう1つのヒント(Andrewが述べたように、これはすべての場合に価値があるとは限りません)Tileクラスで2つの長方形を定義し、フレームごとにxとyの値を変更するだけです

フロア

編集: 色の問題=============================

を使用して、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);
            }
        }
        }

注: _tH=_tileHeight, _tW=_tileWidth これをテストしたところ、私にとって魅力的に機能しました ;) スイッチケースを保持したい場合は、必ず Color.White を着色色 (最後のスプライトバッチパラメーター) として設定してください。また、マップ定義では 0 と 2 を使用していますが、switch ケースでは 0 と 1 を使用しているため、switch ケースの値を修正する必要があります。ケースを 3 つまで拡張することもできました (今では 4 つのタイルがあるようです)。

あなたの問題は、間違った色とケース構造のタイプミスに基づいていたと思います.

お役に立てれば :)

于 2012-09-26T06:47:33.950 に答える