2016-11-25 20 views
0

你好!玩家移動時紋理保持原位

我正在使用MonoGame在C#上開發2D sidescroller冒險遊戲,而且我遇到了一個奇怪的問題。

簡而言之:玩家實例已經分配了一個包含玩家紋理的Texture2D(在這個例子中是一個2px綠色邊框的藍色框) 當玩家移動時,紋理「保持原位」,玩家逐漸變綠,紋理所在的空間。 這當然不應該發生,因爲每次調用Draw()函數時,紋理都應該在玩家的確切位置重新繪製。

換句話說 - 無論他在哪裏移動,玩家都應該是一個帶有2px綠色邊框的藍色方塊。

圖像爲了更好地說明:

原始狀態

original state

球員向下移動,留下身後的質感。

player moves downward, leaving texture behind

continuation of the same thing

直到他完全是綠色的。

player fell down and is green

而當他回到他原來的位置,正確的質地開始出現再次

player returns to original position and turns blue again

我剪下來的代碼,它的基本框架,但問題仍然存在。

Game.cs包含什麼有趣的,播放器初始化並在這裏繪製方法:這裏

//inside of Initialize() 
player = new Player(GraphicsDevice, Vector2.Zero); 


protected override void Draw(GameTime gameTime) { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     levelSpriteBatch.Begin(); 
     player.Draw(gameTime, levelSpriteBatch); 
     levelSpriteBatch.End(); 

     base.Draw(gameTime); 
    } 

Player.cs(整個文件):

class Player { 
     public Vector2 Position 
     { 
      get { return position; } 
     } 
     Vector2 position; 

    public Texture2D Texture { get; private set; } 

    public const int Width = 32; 
    public const int Height = 32; 

    // Input configuration 
    private const Keys leftButton = Keys.A; 
    private const Keys rightButton = Keys.D; 
    private const Keys upButton = Keys.W; 
    private const Keys downButton = Keys.S; 


    //Current state 
    private float LRmovement; 
    private float UDmovement; 

    public Player(GraphicsDevice graphicsDevice, Vector2 position) { 
     this.position = position; 
     CreateTexture(graphicsDevice, Width, Height); 
    } 

    public void Update(GameTime gameTime, KeyboardState keyboardState) { 
     GetInput(keyboardState); 

     position.X += LRmovement; 
     position.Y += UDmovement; 

     LRmovement = 0.0f; 
     UDmovement = 0.0f; 
    } 

    private void CreateTexture(GraphicsDevice graphicsDevice, int width, int height) { 
     Texture = new Texture2D(graphicsDevice, width, height); 

     GraphicsHelper.FillRectangle(Texture, Color.Red); 
     GraphicsHelper.OutlineRectangle(Texture, Color.Green, 2); 
    } 

    private void GetInput(KeyboardState keyboardState) { 
     LRmovement = 0; 
     UDmovement = 0; 

     if (Math.Abs(LRmovement) < 0.5f) 
      LRmovement = 0.0f; 

     if (Math.Abs(UDmovement) < 0.5f) 
      UDmovement = 0.0f; 

     if (keyboardState.IsKeyDown(leftButton)) 
      LRmovement = -1.0f; 
     else if (keyboardState.IsKeyDown(rightButton)) 
      LRmovement = 1.0f; 

     if (keyboardState.IsKeyDown(upButton)) 
      UDmovement = -1.0f; 
     else if (keyboardState.IsKeyDown(downButton)) 
      UDmovement = 1.0f; 
    } 

    public void Draw(GameTime gameTime, SpriteBatch spriteBatch) { 
     spriteBatch.Draw(Texture, position, new Rectangle((int)Position.X, (int)Position.Y, Width, Height), Color.White); 
    } 
} 

而且GraphicsHelper.cs:

class GraphicsHelper { 
    public static void FillRectangle(Texture2D texture, Color fill) { 
     Color[] color = new Color[texture.Width * texture.Height]; 
     texture.GetData(color); 

     for (int i = 0; i < texture.Width * texture.Height; ++i) 
      color[i] = fill; 

     texture.SetData(color); 
    } 

    public static void OutlineRectangle(Texture2D texture, Color outline, int outlineWidth) { 
     Color[] color = new Color[texture.Width * texture.Height]; 
     texture.GetData(color); 

     int index = 0; 
     for (int y = 0; y < texture.Height; ++y) { 
      for (int x = 0; x < texture.Width; ++x) { 
       if (y < outlineWidth || x < outlineWidth || y > texture.Height - outlineWidth || x > texture.Width - outlineWidth) 
        color[index] = outline; 
       ++index; 
      } 
     } 
     texture.SetData(color); 
    } 
} 

這是所有的代碼是。我真的沒有想法。

回答

2

這是有問題的線路:

spriteBatch.Draw(Texture, position, new Rectangle((int)Position.X, (int)Position.Y, Width, Height), Color.White); 

第三個參數(矩形)定義紋理繪製的哪一部分。你總是想繪製紋理的相同部分,因此你應該傳遞一個不變的矩形。

其實,如果你只是想繪製整個紋理,通過null

spriteBatch.Draw(Texture, position, null, Color.White);