2011-08-15 57 views
-5

好了,所以这是代码: 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; } 

} 

Player类:

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)。

顺便说一句,如果任何人都可以给我一些建议,如果我犯了什么错误,或任何我应该改变的,这将是伟大的!

非常感谢帮手。

编辑:任何人..请吗?我想这不是很难找出> <。

+0

你需要调用“base.Draw方法(gameTime );”之前“spriteBatch.End();” – Olle89

+0

@ Olle89这是不正确的。虽然它可能不会崩溃,但您通常不打算用打开的SpriteBatch调用继承的XNA功能。 –

+0

对您的问题进行了一些编辑。花一点时间看看我做了什么。 – Will

回答

1

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

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

应该是

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

是的,这是我的一个错误。我想到了另一个。下一条评论: – Joey

+0

其实还没算出来,还是一样的地方(0,0) – Joey

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

,看看它是如何为你的作品(它应该工作的罚款):)

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

就我个人而言,我从来没有成为gameobject的粉丝,创建自己的基础类型要好得多,因为您可以完全控制它们的工作方式,并且可以更轻松地解决弹出的问题。

+0

这不正是他所做的吗?创建他自己的基类。 GameObject不是内置的XNA类=) – Moulde

+0

哦,我的不好,我把它和GameComponent混淆了......是的,他的GameObject类对我来说看起来并不令人印象深刻。 – tweetypi

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

第二个问题是您的Position属性。当你使用{get;组; }语法编译器隐式给你一个后台字段来存储属性的值。但是你也在创建自己的职位领域。您的位置字段被设置为在构造函数中传递的值(假设您已经在构造函数中修复了问题#1),但Position属性的getter将返回编译器生成的字段,但您尚未设置该字段任何东西。

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

fix选项#1:只需使用位置属性

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

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

fix选项#2:改变位置,避免财产使用你的领域

protected Vector2 Position { get { return position; } set { position = value; } } 
相关问题