2012-06-12 34 views
0

我有一個雲和一個人精靈,這些都是分開繪製,但由於某些原因,他們一起繪製並重疊,然後將自己定位在兩個精靈的定義位置。圖像重疊和繪製兩次

我的精靈類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 

namespace MarioFake 
{ 
class Sprites 
{ 

    public Vector2 MarioPosition = new Vector2(200, 200); 
    private Texture2D MarioStill; 

    public Vector2 LargeCloudPosition = new Vector2(100, 100); 
    private Texture2D LargeCloud; 

    public void LoadContent(ContentManager theContentManager, string theAssetName) 
    { 
     MarioStill = theContentManager.Load<Texture2D>(theAssetName); 
     LargeCloud = theContentManager.Load<Texture2D>(theAssetName); 
    } 

    public void Draw(SpriteBatch theSpriteBatch) 
    { 
     theSpriteBatch.Draw(MarioStill, MarioPosition, Color.White); 
     theSpriteBatch.Draw(LargeCloud, LargeCloudPosition, Color.White); 
    } 

} 
} 

,並在我的遊戲類,我的畫法:

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

     spriteBatch.Begin(); 

     MarioStill.Draw(this.spriteBatch); 
     LargeCloud.Draw(this.spriteBatch); 

     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 

回答

2

Sprite類不一樣,如果你需要保持信息都馬里奧和雲,創建一個這樣的通用雪碧類。

public class Sprite 
    { 
     public Vector2 Location; 
     public Texture2D Texture; 

     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(Texture, Location, Color.White); 
     } 
    } 

你可以創建你的馬里奧和雲這樣。

Sprite Mario = new Sprite() { Location = new Vector2(200, 200), Texture = Content.Load<Texture2D>("MarioTexture") }; 
Sprite Cloud = new Sprite() { Location = new Vector2(100, 100), Texture = Content.Load<Texture2D>("CloudTexture") }; 

並按照以前的方式繪製它們。

  Mario.Draw(spriteBatch); 
      Cloud.Draw(spriteBatch); 

這裏是一個完整的遊戲類的例子,演示加載和渲染兩個精靈。

public class Sprite 
    { 
     public Vector2 Location; 
     public Texture2D Texture; 

     public void Draw(SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(Texture, Location, Color.White); 
     } 
    } 

    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 

     List<Sprite> sprites; 

     Sprite mario, cloud; 

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

     protected override void LoadContent() 
     { 
      spriteBatch = new SpriteBatch(GraphicsDevice); 

      // Create the sprites.    
      sprites = new List<Sprite>(); 
      mario = new Sprite() { Location = new Vector2(100, 100), Texture = Content.Load<Texture2D>("MarioTexture") }; 
      cloud = new Sprite() { Location = new Vector2(200, 200), Texture = Content.Load<Texture2D>("CloudTexture") }; 
      sprites.Add(mario); 
      sprites.Add(cloud); 
     } 

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

      spriteBatch.Begin(); 
      foreach (var sprite in sprites) 
       sprite.Draw(spriteBatch); 
      spriteBatch.End(); 

      base.Draw(gameTime); 
     } 
    } 
+0

這豈不意味着每一個形象我就必須有'雪碧雲=新的雪碧(){位置=新Vector2(100,100),紋理= Content.Load ( 「CloudTexture」)} ' –

+0

或者我想加載的每個精靈都有類似的問題 –

+0

還有加載內容的問題。 –