1

我正在尝试和朋友一起编写游戏,但我对如何绘制精灵感到非常困惑。我可以在下面的 Game1 类中绘制代码。

namespace SweetGeorgiaBrown
{
    /// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
            enum GameStates { TitleScreen, Overworld, Menu, Battle, GameOver };
    GameStates gameState = GameStates.Battle;
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D dinoTex;
    Texture2D alienTex;
    Texture2D robotTex;
    Texture2D eroboTex;
    SpriteFont basic;


    private Vector2 textLocation = new Vector2(20, 20);
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";


    }

    public class Health
    {
        public static int dinoHP = 300;
        public static int alienHP = 200;
        public static int robotHP = 100;
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        dinoTex = Content.Load<Texture2D>(@"Sprites\dino\dinoFrontOvwld");
        alienTex = Content.Load<Texture2D>(@"Sprites\alien\alienFrontOvwld");
        robotTex = Content.Load<Texture2D>(@"Sprites\robot\robotFrontOvwld");
        eroboTex = Content.Load<Texture2D>(@"Sprites\robo1\robo1OvwldFront");

        basic = Content.Load<SpriteFont>(@"Fonts\Basic");
        // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        // TODO: Add your update logic here
        switch (gameState)
        {
            case GameStates.Battle:
                break;
        }
        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();

        // TODO: Add your drawing code here
        if ((gameState == GameStates.Battle))
        {
            spriteBatch.DrawString(
                basic,
                "ASDFASDFADFASDFA ", textLocation, Color.Black);
            spriteBatch.Draw(
                dinoTex, textLocation, Color.White);
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

}

在我的 Game1 代码中,我可以成功地在屏幕上绘制该精灵和文本。但是,我想在另一个类中绘制一个精灵,但我完全不知道该放什么。

在另一堂课上,我希望我能做一些简单的事情

sprite.Draw(//这里将是精灵所在位置的 x 和 y 坐标,纹理的名称),就是这样,但它似乎不能那样工作。有谁知道我该怎么做?

4

1 回答 1

3

它确实是这样工作的。将您的 sprite 特定代码提取到一个结构类似于以下内容的类中:

public Sprite()
{
}

public void Update(GameTime gameTime)
{
}

public void Draw(SpriteBatch spriteBatch)
{
}

然后在你的 game1 类中你可以调用

Sprite.Update(gameTime); 

Sprite.Draw(spriteBatch);

这应该可以帮助您入门。您是否自己尝试了一些方法,如果是这样发布它们,我们可以看到它们哪里出错了,或者您只是希望快速得到答案,您可以复制和粘贴?

于 2012-04-23T16:06:08.113 回答