为什么我的纹理原点在运行时不正确?

时间:2011-08-15 18:49:53

标签: c# xna

好的,所以这是代码: 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)(TOP LEFT)。而不是(700,700)。

顺便说一句,如果我做错了什么都可以给我一些建议,或者我应该改变什么,那就太棒了!

非常感谢帮助者。

编辑:有人......拜托?我想要解决它&gt;&lt;。

并不是那么难

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

我看到两个问题。第一个,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; }

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

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

修复选项#1:只使用位置属性

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

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

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

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

答案 2 :(得分:0)

-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);
}

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

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

就我个人而言,我从未成为游戏对象的粉丝,因为你可以完全控制它们的工作方式并且可以更轻松地解决弹出的问题,所以更好地创建自己的基类型。