Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的瓷砖变小了?_C#_Visual Studio 2010_Xna_Tile_Tile Engine - Fatal编程技术网

C# 为什么我的瓷砖变小了?

C# 为什么我的瓷砖变小了?,c#,visual-studio-2010,xna,tile,tile-engine,C#,Visual Studio 2010,Xna,Tile,Tile Engine,我目前正在做一个游戏,我将在学校里为我的软件工程课程做演示。说实话,我在C#方面没有经验,这就是为什么我要跟随。但是当我在游戏中添加瓷砖时,我的瓷砖被渲染得非常小。我甚至使用了256 x 256的图像。就像在教程视频中一样 您可以看到问题的屏幕截图。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna

我目前正在做一个游戏,我将在学校里为我的软件工程课程做演示。说实话,我在C#方面没有经验,这就是为什么我要跟随。但是当我在游戏中添加瓷砖时,我的瓷砖被渲染得非常小。我甚至使用了256 x 256的图像。就像在教程视频中一样

您可以看到问题的屏幕截图。这是我的密码:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TileEngine
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        List<Texture2D> tileTextures = new List<Texture2D>();

        int[,] tileMap = new int[,]
        {
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
        };

        int tileWidth = 64;
        int tileHeight = 64;

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

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Texture2D texture;

            texture = Content.Load<Texture2D>("Tiles/se_free_dirt_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_grass_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_ground_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_mud_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_road_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_rock_texture");
            tileTextures.Add(texture);

            texture = Content.Load<Texture2D>("Tiles/se_free_wood_texture");
            tileTextures.Add(texture);
        }

        protected override void UnloadContent() { }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

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

            spriteBatch.Begin();

            int tileMapWidth = tileMap.GetLength(1);
            int tileMapHeight = tileMap.GetLength(0);

            for (int x = 0; x < tileMapWidth; x++)
            {
                for (int y = 0; y < tileMapHeight; y++)
                {
                    int textureIndex = tileMap[y, x];
                    Texture2D texture = tileTextures[textureIndex];

                    spriteBatch.Draw(
                        texture,
                        new Rectangle(x * tileWidth, y * tileHeight, tileMapWidth, tileMapHeight),
                        Color.White);
                }
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
命名空间TileEngine
{
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
List tileTextures=新列表();
int[,]tileMap=新int[,]
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
};
int tileWidth=64;
int-tileHeight=64;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
纹理2D纹理;
纹理=内容。加载(“瓷砖/无污垢纹理”);
添加(纹理);
纹理=内容。加载(“瓷砖/无纹理的草纹理”);
添加(纹理);
纹理=内容。加载(“瓷砖/无纹理”地面纹理);
添加(纹理);
纹理=内容。加载(“瓷砖/无泥纹理”);
添加(纹理);
纹理=内容。加载(“瓷砖/无纹理道路纹理”);
添加(纹理);
纹理=内容。加载(“瓷砖/无纹理的岩石纹理”);
添加(纹理);
纹理=内容。加载(“瓷砖/无纹理木材纹理”);
添加(纹理);
}
受保护的覆盖无效UnloadContent(){}
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
int tileMapWidth=tileMap.GetLength(1);
int tileMapHeight=tileMap.GetLength(0);
对于(int x=0;x
更改每个瓷砖的宽度和高度,使其更大:

int tileWidth = 300;
int tileHeight = 300;
基本上,任何数字加起来都会产生你想要的大小

此外,矩形是通过添加起点+大小来形成的,因此大小也需要与点匹配,否则它们将相互重叠

例如,如果要以正方形渲染,则titleWidth应与tileMapWidth相同

目前看来,矩阵的长度肯定是错误的,因为这个数字很小(10),并且会呈现小正方形(10x10)

因此,请执行以下操作:

int tileMapWidth = tileWidth;
int tileMapHeight = tileHeight;
而不是

int tileMapWidth = tileMap.GetLength(1);
int tileMapHeight = tileMap.GetLength(0);

您只需替换下面块中的第3行:

spriteBatch.Draw(
    texture,
    new Rectangle(x * tileWidth, y * tileHeight, tileMapWidth, tileMapHeight),
    Color.White);
所以它就像下一块的第三行:

spriteBatch.Draw(
    texture,
    new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight),
    Color.White);
所以问题是:在代码中,纹理的矩形宽度和高度被设置为贴图的宽度和高度-这似乎比纹理图像的大小小得多。

您可以这样尝试

width = 64 ' or texture.width
height = 64 ' or texture.height
pos = new vector2d(x * width , y * height)
spriteBatch.Draw(texture, pos, Color.White);

如果x从0开始,则第一个纹理将在0,0秒的64,0

谢谢您的快速回复,先生,我按照您的要求做了,这就是结果,它只是在瓷砖之间增加了更多的间隙:(看起来瓷砖已绘制,但您使用的纹理会产生此结果。请尝试将纹理替换为简单颜色和简单边框哦!此外,您的矩形函数看起来错误。请检查我的更新响应。)