2012-06-12 74 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

还有加载内容的问题。 –