簡體   English   中英

XNA Sprite似乎沒有更新?

[英]XNA Sprite doesn't seem to be updating?

我已經瀏覽了其他一些問題,但沒有找到答案,所以我問你是可愛的人,因為你以前曾幫助過我:)

我有一個主類(RPG.cs)和一個播放器類(Player.cs),並且我希望播放器類盡可能自成體系(因為我不希望使用huuuge主類),但這是我的問題。

問題

我的播放器精靈可以很好地繪制,但是當我希望它移動時,它不會更新! 目前,我正在嘗試使其像光標一樣測試運動,但最終我將其綁定到WASD或箭頭鍵。 所以我現在想讓精靈跟隨我的鼠標,但是它只停留在50、50(它是預設的起始位置)上嗎?我錯過了明顯的東西嗎? 我是XNA的新手,已經花了半小時解決這個問題!

角色扮演游戲

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 TestGame
{
    public class RPG : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D PlayerTex;
        Rectangle playerPos = new Rectangle(
            Convert.ToInt32(Player.Pos.X), 
            Convert.ToInt32(Player.Pos.Y), 32, 32);
        public RPG()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }
        protected override void Initialize() { base.Initialize(); }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            PlayerTex = Content.Load<Texture2D>("testChar");
        }
        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.White);
            spriteBatch.Begin();
            spriteBatch.Draw(PlayerTex, playerPos, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

Player.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 TestGame
{
    /// This is a game component that implements IUpdateable.
    public class Player : Microsoft.Xna.Framework.GameComponent
    {
        public static Vector2 Pos = new Vector2(50, 50);
        MouseState ms = Mouse.GetState();
        public Player(Game game) : base(game) { }
        /// Allows the game component to perform 
        /// any initialization it needs to before 
        /// starting to run. This is where it can 
        /// query for any required services and load content.
        public override void Initialize() { base.Initialize(); }
        /// Allows the game component to update itself.
        public override void Update(GameTime gameTime)
        {
            Pos.X = ms.X;
            Pos.Y = ms.Y;
            base.Update(gameTime);
        }
    }
}

希望能對您有所幫助! :)

如評論中所述,我不會解決所有問題。 但是主要錯誤。

讓玩家對自己的位置而不是游戲負責。 此外,我會讓玩家自己負責繪畫,但這對這個答案來說有點太過分了。

以下代碼至少應該起作用。

public class Player : Microsoft.Xna.Framework.GameComponent
{
    public Vector2 Pos { get; set; }

    public Player(Game game) : base(game)
    {
        this.Pos = new Vector2(50, 50);
    }

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

    public override void Update(GameTime gameTime)
    {
        var ms = Mouse.GetState();
        Pos.X = ms.X;
        Pos.Y = ms.Y;
        base.Update(gameTime);
    }
}

然后在游戲中使用玩家:

public class RPG : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D PlayerTex;

    Player player;

    public RPG()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
        player = new Player(this);
        Components.Add(player);
    }
    protected override void Initialize() { base.Initialize(); }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        PlayerTex = Content.Load<Texture2D>("testChar");
    }
    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.White);
        spriteBatch.Begin();
        spriteBatch.Draw(PlayerTex, player.Pos, Color.White);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

我已經刪除了紋理的大小。 不知道您是否真的需要這個。 如果是這樣,您可以讓Player暴露一個Rectangle,而不僅僅是Vector2

Nico包括的關鍵解決方案就是您使用繪制時所用的原始矩形坐標:

Rectangle playerPos = new Rectangle(
             Convert.ToInt32(Player.Pos.X), 
             Convert.ToInt32(Player.Pos.Y), 32, 32);

在這里,您可以使用玩家當前的開始時間位置來制作矩形。 然后,您總是根據您以前制作的且從未更新過的矩形繪制:

        spriteBatch.Draw(PlayerTex, playerPos, Color.White);

正如Nico所說的正確方法就是將平局更改為此:

 playerPos = new Rectangle(
             Convert.ToInt32(Player.Pos.X), 
             Convert.ToInt32(Player.Pos.Y), 32, 32);

    spriteBatch.Draw(PlayerTex, playerPos, Color.White);

現在,您將每次都在一個新的地方繪圖。 有更好的方法可以做到這一點(例如nico所做的事情),但這是核心思想。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM