我怎样才能画在它自己的类中的精灵?(How can I draw a sprite in it

2019-09-30 04:14发布

我想和朋友一起游戏程序,我为如何绘制精灵很困惑。 我可以在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 coords)使用,并有那也罢了,但它似乎只是不这样的。 有谁知道我怎么能去这样做?

Answer 1:

它确实工作这样。 提取你的精灵特定代码,这样的结构类似的一类:

public Sprite()
{
}

public void Update(GameTime gameTime)
{
}

public void Draw(SpriteBatch spriteBatch)
{
}

然后在您的Game1类可以调用

Sprite.Update(gameTime); 

Sprite.Draw(spriteBatch);

这应该帮助您开始。 你尝试一些方法自己,如果是的话张贴,我们可以看到他们去哪里错了,还是你只是希望尽快得到答案,你可以复制和粘贴?



文章来源: How can I draw a sprite in it's own class?