【问题标题】:Why is my Texture origin is incorrect at runtime?为什么我的纹理原点在运行时不正确?
【发布时间】:2011-08-15 18:49:52
【问题描述】:

好的,代码如下: Game1.cs 类:

 public class Game1 : Microsoft.Xna.Framework.Game
{
    KeyboardState keyboard;
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Player MyPlayer;
    Texture2D Ball;

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

    protected override void Initialize()
    {
        IsMouseVisible = true;
        graphics.IsFullScreen = true;

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Ball = Content.Load<Texture2D>("Images/Ball");
        MyPlayer = new Player(new Vector2(700, 700), Ball);

    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        keyboard = Keyboard.GetState();
        if (keyboard.IsKeyDown(Keys.Escape))
            this.Exit();
        MyPlayer.Update(gameTime);

        base.Update(gameTime);
    }

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

游戏对象类:

 abstract class GameObject
{
    protected Vector2 position;
    protected Texture2D texture;
    public GameObject(Vector2 Position, Texture2D Texture)
    {
        position = Vector2.Zero;
        this.texture = Texture;
    }
    protected Vector2 Position { set; get; }
    protected Texture2D Texture { set; get; }
    protected float X
    {
        set { position.X = value; }
        get { return position.X; }
    }
    protected float Y
    {
        set
        {
            position.Y = value;
        }
        get
        {
            return position.Y;
        }
    }
    public int GraphicsWidth { set; get; }

    public int GraphicsHeight { set; get; }

}

玩家等级:

class Player : GameObject
{
    KeyboardState keyboard;
    bool IsJump = false;
    int SpeedX = 5;
    int SpeedY = 5;

    public Player(Vector2 position, Texture2D tex):base(position,tex)
    {
    }
    public int Height
    {
        get { return this.texture.Height; }
    }
    public int Width
    {
        get { return this.texture.Width; }
    }
    public void intialize()
    {

    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, Position, Color.White);
    }
    public void Update(GameTime gameTime)
    {

        keyboard = Keyboard.GetState();
        if (keyboard.IsKeyDown(Keys.D))
        {
            X += SpeedX;
        }
        else if (keyboard.IsKeyDown(Keys.A))
        {
            X-= SpeedX;
        }
        if (X > GraphicsWidth)
        {
            X = GraphicsWidth;
        }

    }

}

当我调试它时,我的球纹理位于 (0,0)(左上角)。而不是 (700,700)。

顺便说一句,如果我做错了什么或者我应该改变什么,如果有人能给我一些建议,那就太好了!!

非常感谢帮助。

编辑:任何人..拜托?我想这并不难弄清楚><.>

【问题讨论】:

  • 你需要调用“base.Draw(gameTime);”在“spriteBatch.End();”之前
  • @Olle89 这是不正确的。虽然它可能不会崩溃,但您通常不希望使用打开的 SpriteBatch 调用继承的 XNA 功能。
  • 对您的问题进行了一些修改。花点时间看看我做了什么。

标签: c# xna


【解决方案1】:

在您的 GameObject 构造函数中,您将位置设置为 Vector2.Zero 而不是您的参数位置。

public GameObject(Vector2 Position, Texture2D Texture)
{
    position = Vector2.Zero;
    this.texture = Texture;
}

应该是

public GameObject(Vector2 Position, Texture2D Texture)
{
    position = Position;
    this.texture = Texture;
}

【讨论】:

  • 是的,这是我的错误之一。我想出了另一个。下一条评论:
  • 其实还没想好,还是同一个地方(0,0)
【解决方案2】:

我看到了两个问题。第一个,由 CraigW 回答,你已经修复了:

protected Vector2 position;    

public GameObject(Vector2 Position, Texture2D Texture)
{
    position = Vector2.Zero;  // <---- this needs to be position = Position
    this.texture = Texture;    
}

protected Vector2 Position { set; get; }

第二个问题是您的 Position 属性。当您使用 { get; 声明属性时放; } 语法编译器隐含地给你一个支持字段来存储属性的值。但是您也在创建自己的职位字段。您的 position 字段被设置为在构造函数中传递的值(假设您已在构造函数中修复了问题 #1),但您的 Position 属性的 getter 正在返回编译器生成的字段,您尚未设置为任何东西。

有两种方法可以解决此问题。删除您的职位字段并在任何地方引用 Position 属性,或者修改您的属性以使用您的职位字段。

修复选项 #1:只需使用 Position 属性

// protected Vector2 position;    // <----- remove this

public GameObject(Vector2 Position, Texture2D Texture)
{
  this.Position = Position;
  this.texture = Texture;
}

修复选项 #2:更改您的 Position 属性以使用您的字段

protected Vector2 Position { get { return position; } set { position = value; } }

【讨论】:

    【解决方案3】:

    -7?你做了什么应得的!?

    我认为您的代码中存在逻辑错误,请更改此:

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, Position, Color.White);
    }
    

    到这里:

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, new Vector2(700,700), Color.White);
    }
    

    看看它是如何为你工作的(它应该可以正常工作):)

    从那里你将不得不向后工作以找到位置没有被设置为你想要的值。

    就我个人而言,我从来都不是游戏对象的粉丝,最好创建自己的基本类型,因为您可以完全控制它们的工作方式,并且可以更轻松地解决出现的问题。

    【讨论】:

    • 这不正是他所做的吗?创建他自己的基类。 GameObject 不是内置的 XNA class=)
    • 哦,我的错,我把它和 GameComponent 搞混了……是的,他的 GameObject 类在我看来并不令人印象深刻,哈哈。
    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多