1

.txtファイルの数値に対応するスプライトを描画する単純なタイルエンジンを使用して小さなゲームを実行しました。そして今、私は前進したいので、ゲームが.pngか何かを読み取り、すべてのピクセルに対してスプライトを描画するように作成したいと思います。また、画像内の色が異なると、異なるスプライトが描画されます。誰かがこれを手伝ってくれる?また、これはC#XNA4.0で行っています。

4

2 に答える 2

1

まず、pipleineを使用して画像をTexture2Dにロードします。次に、コードでtexture2d.GetDataを使用してピクセルの色を取得します。

MSDNtexture2d.GetDataの例http://msdn.microsoft.com/en-us/library/bb197093.aspx

于 2012-11-28T17:55:07.757 に答える
1

Texture2DTexture2D.GetDataであるため、タイルマップを作成するために使用する場合(マップはタイルの2Dグリッドであると想定しています)、使用するのは難しい場合があります。GetDataは1D配列を返します。

まず、マップ用の配列が必要になりますが、保存すると次のようになります。

Color[,] tiles = new Color[LEVEL_WIDTH,LEVEL_HEIGHT];

ファイルから事前に作成された構造をロードするために、次の手法を使用しました

//Load the texture from the content pipeline
Texture2D texture = Content.Load<Texture2D>("Your Texture Name and Directory");

//Convert the 1D array, to a 2D array for accessing data easily (Much easier to do Colors[x,y] than Colors[i],because it specifies an easy to read pixel)
Color[,] Colors = TextureTo2DArray(texture);

そして機能...

    Color[,] TextureTo2DArray(Texture2D texture)
    {
        Color[] colors1D = new Color[texture.Width * texture.Height]; //The hard to read,1D array
        texture.GetData(colors1D); //Get the colors and add them to the array

        Color[,] colors2D = new Color[texture.Width, texture.Height]; //The new, easy to read 2D array
        for (int x = 0; x < texture.Width; x++) //Convert!
            for (int y = 0; y < texture.Height; y++)
                colors2D[x, y] = colors1D[x + y * texture.Width];

        return colors2D; //Done!
    }

ここで、マップを色に設定することができます。そうするだけで、 !tiles = Colorsを使用して配列からデータに簡単にアクセスできます。Colors[x,y]

例:

 using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace YourNameSpace
{
    class Game
    {
        //"Global" array for the map, this holds each tile
        Color[,] tiles = new Color[LEVEL_WIDTH, LEVEL_HEIGHT];
        protected override void Initialize() //OR wherever you load the map and stuff
        {
            //Load the texture from the content pipeline
            Texture2D texture = Content.Load<Texture2D>("Your Texture Name and Directory");

            //Convert the 1D array, to a 2D array for accessing data easily (Much easier to do Colors[x,y] than Colors[i],because it specifies an easy to read pixel)
            Color[,] Colors = TextureTo2DArray(texture);
        }
        Color[,] TextureTo2DArray(Texture2D texture)
        { //ADD THE REST, I REMOVED TO SAVE SPACE
        }
    }
}
于 2012-11-28T21:07:30.300 に答える