【问题标题】:How can I draw a sprite in it's own class?我怎样才能在它自己的类中绘制一个精灵?
【发布时间】:2012-04-24 00:06:08
【问题描述】:

我正在尝试和朋友一起编写游戏,但我对如何绘制精灵感到非常困惑。我可以在下面的 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 坐标,纹理的名称),就是这样,但它似乎不能那样工作。有谁知道我该怎么做?

【问题讨论】:

    标签: c# graphics xna sprite


    【解决方案1】:

    确实如此。将您的 sprite 特定代码提取到一个结构类似于以下内容的类中:

    public Sprite()
    {
    }
    
    public void Update(GameTime gameTime)
    {
    }
    
    public void Draw(SpriteBatch spriteBatch)
    {
    }
    

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

    Sprite.Update(gameTime); 
    

    Sprite.Draw(spriteBatch);
    

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

    【讨论】:

    • 我删除了其中的大部分,因为它们不起作用,但我尝试做类似 Game1.Draw(spritename) 之类的操作,或者我尝试编写自己的 Draw 代码的地方更长,但我不包括更新,因此也不起作用。感谢您的时间和帮助。
    • 仍然对我现在应该做的事情感到非常困惑。我创建了一个名为 SpriteDraw 的类,在其中我有 public SpriteDraw( Vector2 location, Texture2D texture) { } public void Update(GameTime gameTime) { } public void Draw(SpriteBatch spriteBatch) { } 我不知道在 Game1 的哪个位置应该把 SpiteDraw.Draw(spriteBatch);和 SpriteDraw.Update(gameTime),因为它每次都给我一个错误。我尝试通过输入将其放入 LoadContent
    • spriteDraw = new SpriteDraw(new Vector2(0, 30), new Texture2D"@/Sprites/dino/dinobackOvwld.png" ));但这似乎也不起作用
    • 我不知道如何让它显示一个精灵。我只是通过另一个不是 SpriteDraw 的类来调用它吗?在 LoadContent 中,我输入 spriteDraw = new SpriteDraw(textLocation, robotTex);我把 spriteDraw.Draw(spriteBatch) 放在 'if((gameState = GameStates.Battle))' 代码中,我把更新代码放在 'case GameStates.Battle:' 下
    • 从 GameObject 导出你的精灵
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多