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

1 回答 1

2

如果你创建一个像这样的通用精灵类,精灵类不需要保存马里奥和云的信息。

  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);
        }
    }
于 2012-06-12T14:03:01.307 回答