在XNA 4.0中难以加载纹理并将其传递给类

本文关键字:纹理 加载 XNA | 更新日期: 2023-09-27 18:12:57

这是c#/XNA 4.0类的作业,不,我不是hwgrade僵尸。我想让你知道我问这个问题的具体情况,我做了多少"研究",我希望学习而不是得到一个分数,但这会被认为是废话,被删除。

总之,我的问题是,我的内容(一个精灵表),我试图加载到"textureImage"在Game1.cs,然后传递给"sprite .cs"被绘制,通过"userControlledSprite.cs",并没有被绘制当我编译我的程序。作为c#和XNA的新手,我理解这个问题(内容加载,但没有正确传递),但我不知道如何解决它。

下面是我的程序包含的全部三个类:

Game1.cs——调用userControlledSprite。

    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 Proj06
    {
        public class Game1 : Microsoft.Xna.Framework.Game
        {
           GraphicsDeviceManager graphics;
           SpriteBatch spriteBatch;
            static Texture2D textureImage;
            static Point frameSize = new Point(52,50);
            static Point currentFrame = new Point(0, 0);
            static Point sheetSize = new Point(4, 1);
            static Vector2 position = Vector2.Zero;
            static int collisionOffset = 1;
            static Vector2 speed;
           userControlledSprite UserControlledSprite1;

        public Game1()
        {
           graphics = new GraphicsDeviceManager(this);
           Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
           base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            textureImage = Content.Load<Texture2D>(@"images/Picture");
            UserControlledSprite1 = new userControlledSprite(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed);
        }
        protected override void UnloadContent()
        {
          /// Ignore this void
        }
        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.White);
            UserControlledSprite1.Draw(gameTime, spriteBatch);
            base.Draw(gameTime);
        }
    }
}

sprite .cs—绘制和动画精灵,以及检查碰撞。

    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 Proj06
    {
       abstract class Sprite
        {
            Texture2D textureImage;
            protected Point frameSize;
            Point currentFrame;
            Point sheetSize;
            int collisionOffset;
            int timeSinceLastFrame = 0;
            int millisecondsPerFrame;
            const int defaultMillisecondsPerFrame = 16;
            protected Vector2 speed;
            protected Vector2 position;
            public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
                int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed)
                : this(textureImage, position, frameSize, collisionOffset, currentFrame,
                sheetSize, speed, defaultMillisecondsPerFrame)
            {
            }
            public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
                int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
                int millisecondsPerFrame)
            {
                this.textureImage = textureImage;
                this.position = position;
                this.frameSize = frameSize;
                this.collisionOffset = collisionOffset;
                this.currentFrame = currentFrame;
                this.sheetSize = sheetSize;
                this.speed = speed;
                this.millisecondsPerFrame = millisecondsPerFrame;
            }
            public virtual void Update(GameTime gameTime, Rectangle clientBounds)
            {
                timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                if (timeSinceLastFrame > millisecondsPerFrame)
                {
                    timeSinceLastFrame = 0;
                    ++currentFrame.X;
                    if (currentFrame.X >= sheetSize.X)
                    {
                        currentFrame.X = 0;
                        ++currentFrame.Y;
                        if (currentFrame.Y >= sheetSize.Y)
                        {
                            currentFrame.Y = 0;
                        }
                    }
                }
            }
            public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(textureImage,
                    position,
                    new Rectangle(currentFrame.X * frameSize.X,
                        currentFrame.Y * frameSize.Y,
                        frameSize.X, frameSize.Y),
                        Color.White, 0, Vector2.Zero,
                        1f, SpriteEffects.None, 0);
                spriteBatch.End();
            }
            public abstract Vector2 direction
            {
                get;
            }
            public Rectangle collisionRect
            {
                get
                {
                    return new Rectangle(
                        (int)position.X + collisionOffset,
                        (int)position.Y + collisionOffset,
                        frameSize.X - (collisionOffset * 2),
                        frameSize.Y - (collisionOffset * 2));
                }
            }
        }
    }

和userControlledSprite.cs——处理精灵的移动和位置

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    namespace Proj06
    {
        class userControlledSprite : Sprite
        {
            public userControlledSprite(Texture2D textureImage, Vector2 position,
                Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
                Vector2 speed)
                : base(textureImage, position, frameSize, collisionOffset, currentFrame,
                sheetSize, speed)
            {
            }
            public userControlledSprite(Texture2D textureImage, Vector2 position,
                Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
                Vector2 speed, int millisecondsPerFrame)
                : base(textureImage, position, frameSize, collisionOffset, currentFrame,
                sheetSize, speed, millisecondsPerFrame)
            {
            }
            public override Vector2 direction
            {
                get
                {
                    Vector2 inputDirection = Vector2.Zero;
                    if (Keyboard.GetState().IsKeyDown(Keys.Left))
                        inputDirection.X -= 1;
                    if (Keyboard.GetState().IsKeyDown(Keys.Right))
                        inputDirection.X += 1;
                    if (Keyboard.GetState().IsKeyDown(Keys.Up))
                        inputDirection.Y -= 1;
                    if (Keyboard.GetState().IsKeyDown(Keys.Down))
                        inputDirection.Y += 1;
                    return inputDirection * speed;
                }
            }
            public override void Update(GameTime gameTime, Rectangle clientBounds)
            {
                position += direction;
                if (position.X < 0)
                    position.X = 0;
                if (position.Y < 0)
                    position.Y = 0;
                if (position.X > clientBounds.Width - frameSize.X)
                    position.X = clientBounds.Width - frameSize.X;
                if (position.Y > clientBounds.Height - frameSize.Y)
                    position.X = clientBounds.Height - frameSize.Y;
                base.Update(gameTime, clientBounds);
            }
        }
    }
我将继续在网上搜索任何有用的资源,如果我发现了什么,我会发布编辑。感谢您的宝贵时间。

在XNA 4.0中难以加载纹理并将其传递给类

保留UserControlledSprite1的字段定义,但不要在那里初始化它:

public class Game1 : Microsoft.Xna.Framework.Game
{
    userControlledSprite UserControlledSprite1;
}

然后在加载纹理后在LoadContent()方法中初始化它:

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    textureImage = Content.Load<Texture2D>(@"images/Picture");
    UserControlledSprite1 = new userControlledSprite(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed);
}

同时,确保在Game1.Draw()中调用UserControlledSprite1.Draw()方法。

这里很简单,您的DrawUpdate方法永远不会被调用。

如果您希望它们被XNA自动调用,您的Sprite类需要扩展DrawableGameComponent并在Game1中注册。为此,您需要将Game作为参数传递。您将覆盖的Draw方法也需要一个SpriteBatch,因此也要在构造函数中传递它。这不是一个干净的方法,但它会起作用。然后在每个DrawableGameComponent上调用Components.Add

一旦完成,只需在Game1中调用spriteBatch.Begin()和spriteBatch.End()(在Begin和End上只调用一次),每个精灵就会画出自己。

我还冒昧地删除了无用的using s,并修复了您的类名以符合c#约定(PascalCase)。

改变的重要部分是:

  • userControlledSprite现在接收spriteBatch和Game类
  • userControlledSprite注册为组件
  • Sprite现在扩展DrawableGameComponent
  • spriteBatch。开始和结束现在称为
  • Sprite现在覆盖正确的绘制方法。

请注意我没有改变你的Update方法。它不会被调用,除非你为public override void Update(GameTime gameTime)更改它。

如果你在Draw中设置了一个断点,它应该被XNA调用。

Game1.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Proj06
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private Texture2D textureImage;
        private Point frameSize = new Point(52, 50);
        private Point currentFrame = new Point(0, 0);
        private Point sheetSize = new Point(4, 1);
        private Vector2 position = Vector2.Zero;
        private int collisionOffset = 1;
        private Vector2 speed;
        private UserControlledSprite userControlledSprite1;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            textureImage = Content.Load<Texture2D>(@"images/Picture");
            userControlledSprite1 = new UserControlledSprite(this, spriteBatch, textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed);
            Components.Add(userControlledSprite1);
        }
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Exit();
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            base.Draw(gameTime);
            spriteBatch.End();
        }
    }
}

UserControlledSprite.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Proj06
{
    class UserControlledSprite : Sprite
    {
        public UserControlledSprite(Game game, SpriteBatch sb, Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed)
            : base(game, sb, textureImage, position, frameSize, collisionOffset, currentFrame,
            sheetSize, speed)
        {
        }
        public UserControlledSprite(Game game, SpriteBatch sb, Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed, int millisecondsPerFrame)
            : base(game, sb, textureImage, position, frameSize, collisionOffset, currentFrame,
            sheetSize, speed, millisecondsPerFrame)
        {
        }
        public override Vector2 direction
        {
            get
            {
                Vector2 inputDirection = Vector2.Zero;
                if (Keyboard.GetState().IsKeyDown(Keys.Left))
                    inputDirection.X -= 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Right))
                    inputDirection.X += 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                    inputDirection.Y -= 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                    inputDirection.Y += 1;
                return inputDirection * speed;
            }
        }
        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            position += direction;
            if (position.X < 0)
                position.X = 0;
            if (position.Y < 0)
                position.Y = 0;
            if (position.X > clientBounds.Width - frameSize.X)
                position.X = clientBounds.Width - frameSize.X;
            if (position.Y > clientBounds.Height - frameSize.Y)
                position.X = clientBounds.Height - frameSize.Y;
            base.Update(gameTime, clientBounds);
        }
    }
}

Sprite.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Proj06
{
    abstract class Sprite : DrawableGameComponent
    {
        Texture2D textureImage;
        protected Point frameSize;
        Point currentFrame;
        Point sheetSize;
        int collisionOffset;
        int timeSinceLastFrame = 0;
        int millisecondsPerFrame;
        const int defaultMillisecondsPerFrame = 16;
        protected Vector2 speed;
        protected Vector2 position;
        private SpriteBatch spriteBatch;
        public Sprite(Game game, SpriteBatch sb, Texture2D textureImage, Vector2 position, Point frameSize,
            int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed)
            : this(game, sb, textureImage, position, frameSize, collisionOffset, currentFrame,
            sheetSize, speed, defaultMillisecondsPerFrame)
        {
        }
        public Sprite(Game game, SpriteBatch sb, Texture2D textureImage, Vector2 position, Point frameSize,
            int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
            int millisecondsPerFrame) : base(game)
        {
            this.textureImage = textureImage;
            this.position = position;
            this.frameSize = frameSize;
            this.collisionOffset = collisionOffset;
            this.currentFrame = currentFrame;
            this.sheetSize = sheetSize;
            this.speed = speed;
            this.millisecondsPerFrame = millisecondsPerFrame;
            spriteBatch = sb;
        }
        public virtual void Update(GameTime gameTime, Rectangle clientBounds)
        {
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > millisecondsPerFrame)
            {
                timeSinceLastFrame = 0;
                ++currentFrame.X;
                if (currentFrame.X >= sheetSize.X)
                {
                    currentFrame.X = 0;
                    ++currentFrame.Y;
                    if (currentFrame.Y >= sheetSize.Y)
                    {
                        currentFrame.Y = 0;
                    }
                }
            }
        }
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Draw(textureImage,
                position,
                new Rectangle(currentFrame.X * frameSize.X,
                    currentFrame.Y * frameSize.Y,
                    frameSize.X, frameSize.Y),
                    Color.White, 0, Vector2.Zero,
                    1f, SpriteEffects.None, 0);
            base.Draw(gameTime);
        }
        public abstract Vector2 direction
        {
            get;
        }
        public Rectangle collisionRect
        {
            get
            {
                return new Rectangle(
                    (int)position.X + collisionOffset,
                    (int)position.Y + collisionOffset,
                    frameSize.X - (collisionOffset * 2),
                    frameSize.Y - (collisionOffset * 2));
            }
        }
    }
}

作为补充,我强烈建议你转向《一夫一妻制》。XNA不再被支持了(它在很久以前就被微软扼杀了),而MonoGame是开源的实现。没有太大的变化,但是您仍然需要使用XNA的内容管道(. xnb文件)。

如果你想坚持使用XNA,可以使用Platformer Starter Kit启动项目。您将更好地了解XNA的工作原理,因为您的体系结构非常奇怪。