我有一个云和一个人精灵,这些都是分开绘制的,但由于某种原因,它们一起绘制并重叠,然后将它们自己定位在两个精灵的定义位置。
我的精灵课程:
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);
}
}
}
和我在Game类中的绘图方法:
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);
}
答案 0 :(得分:2)
如果你创建一个像这样的通用精灵类,精灵类不需要保存Mario和云的信息。
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);
}
}