Skip to main content
 首页 » 编程设计

c#之为什么我的 Sprite 不绘制 (新华社)

2024年02月23日16zhengyun_ustc

我只是在摆弄 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); 
    } 
} 
} 

提前感谢您的宝贵时间!

请您参考如下方法:

我仔细看了看,您似乎从未定义 Actor 的颜色属性。这意味着 color 变量默认为黑色,这意味着赋予 Draw 线的色调 (spriteBatch.Draw(this.texture, this.position, this.color);) 是黑色的,它隐藏了 Sprite 。

只需将 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; 
} 

希望对你有帮助,
最大