为什么我的精灵画不出来?(XNA)

本文关键字:XNA 我的 精灵 为什么 | 更新日期: 2023-09-27 18:16:04

我只是在摆弄XNA,并试图获得一个可以通过键盘控制移动的简单播放器类。但我好像不能把它画成我现在设置的样子。我看不出我犯了什么错误。

下面是Actor基类:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RunnerTEst
{
    class Actor
    {
        public static List<Actor> actors;
        public Texture2D texture;
        protected Vector2 position;
        protected float rotation;
        protected float scale;
        protected Color color;
    #region Constructors
        static Actor()
        {
            actors = new List<Actor>();
        }
        public Actor(Texture2D texture)
        {
            actors.Add(this);
            this.texture = texture;
            this.position = Vector2.Zero;
            this.rotation = 0f;
            this.scale = 1f;
        }
    #endregion
    #region Properties
        public Vector2 Position 
        {
            get { return this.position; }
            set { this.position = value; }
        }
        public Vector2 Origin
        {
            get { return new Vector2(this.position.X + this.texture.Width / 2,
                                     this.position.Y + this.texture.Height / 2); }
        }
        public float Rotation
        {
            get { return this.rotation; }
            set { this.rotation = value; }
        }
        public float Scale
        {
            get { return this.scale; }
            set { this.scale = value; }
        }
        public Color Color
        {
            get { return this.color; }
            set { this.color = value; }
        }
        public float Width
        {
            get { return this.texture.Width; }
        }
        public float Height
        {
            get { return this.texture.Height; }
        }
    #endregion
    #region Methods
        public virtual void Update(GameTime gameTime)
        {
        }
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.texture, this.position, this.color);
        }
    #endregion
    }
}

球员类:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace RunnerTEst
{
    class Player : Actor
{
    private const float speed = 5f;
    protected Vector2 velocity;
    private static KeyboardState currState, prevState;
    static Player()
    {
        currState = prevState = Keyboard.GetState();
    }
    public Player(Texture2D texture, Vector2 position)
        : base(texture)
    {
        this.position = position;
        this.velocity = Vector2.Zero;
    }
    public override void Update(GameTime gameTime)
    {
        this.HandleInput();
        this.position += this.velocity;
        foreach (Actor actor in Actor.actors)
        {
            if (this == actor)
                continue;
            this.CheckCollision(actor);
        }
        base.Update(gameTime);
    }
    public override void Draw(SpriteBatch spriteBatch)
    {
        base.Draw(spriteBatch);
    }
    public void CheckCollision(Actor actor)
    {
        //--left/right sides
        if (this.position.X + this.Width > actor.Position.X) //right of this hitting left actor
        {
            this.position.X = actor.Position.X - this.Width;
        }
        else if (this.position.X < (actor.Position.X + actor.Width))//left this hit right actor
        {
            this.position.X = actor.Position.X + actor.Width;
        }
        //--top/bottom
        if (this.position.Y + this.Height > actor.Position.Y) //this bottom hit actor top
        {
            this.position.Y = actor.Position.Y - this.Width;
        }
        else if (this.position.Y < (actor.Position.Y + actor.Height))//this top hit actor bottom
        {
            this.position.Y = actor.Position.Y + actor.Height;
        }
        //TODO: check screen bounds
    }
    public void HandleInput()
    {
        currState = Keyboard.GetState();
        if (currState.IsKeyDown(Keys.W))
        {
            this.velocity.Y = -speed;
        }
        else if (currState.IsKeyDown(Keys.S))
        {
            this.velocity.Y = speed;
        }
        else
        {
            this.velocity.Y = 0f;
        }
        if (currState.IsKeyDown(Keys.A))
        {
            this.velocity.X = -speed;
        }
        else if (currState.IsKeyDown(Keys.D))
        {
            this.velocity.X = speed;
        }
        else
        {
            this.velocity.X = 0f;
        }
        prevState = currState;
    }
}
}

最后,游戏:

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 RunnerTEst
{
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D playerTexture;
    Player me;
    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);
        playerTexture = Content.Load<Texture2D>(@"Textures'player");
        me = new Player(playerTexture, new Vector2(200, 200));
    }
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        me.Update(gameTime);

        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        me.Draw(spriteBatch);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
}

提前感谢您的时间!

为什么我的精灵画不出来?(XNA)

我看了一下,看起来你从来没有定义过Actor的color属性。这意味着color变量默认为黑色,这意味着给绘制线(spriteBatch.Draw(this.texture, this.position, this.color);)的色调是黑色,这隐藏了精灵。

将Actor的颜色设置为白色(我只是将它设置在me = new Player(playerTexture, new Vector2(200, 200));下面)。

所以新的LoadContent看起来像:

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    playerTexture = Content.Load<Texture2D>(@"Textures'player");
    me = new Player(playerTexture, new Vector2(200, 200));
    me.Color = Color.White;
}

希望有帮助,
马克斯