我如何在自己的课堂中绘制一个精灵? [英] How can I draw a sprite in it's own class?

查看:143
本文介绍了我如何在自己的课堂中绘制一个精灵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图和一位朋友一起编写游戏,我对如何绘制精灵感到困惑。

 命名空间SweetGeorgiaBrown 
{
///< ;总结>
///这是您游戏的主要类型
///< / summary>
public class Game1:Microsoft.Xna.Framework.Game
{
enum GameStates {TitleScreen,Overworld,Menu,Battle,GameOver};
GameStates gameState = GameStates.Battle;
GraphicsDeviceManager图形;
SpriteBatch spriteBatch;
Texture2D dinoTex;
Texture2D alienTex;
Texture2D robotTex;
Texture2D eroboTex;
SpriteFont基本;


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>
///允许游戏在开始运行之前执行它需要的任何初始化。
///这是它可以查询任何所需服务并加载任何非图形
///相关内容的地方。调用base.Initialize将枚举任何组件
///并初始化它们。
///< / summary>
protected override void Initialize()
{
// TODO:在这里添加初始化逻辑

base.Initialize();
}

///< summary>
/// LoadContent将在每个游戏中调用一次,并且是加载
///所有内容的地方。
///< / summary>
protected override void LoadContent()
{
//创建一个新的SpriteBatch,可以用来绘制纹理。
spriteBatch =新的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:使用this.Content在这里加载您的游戏内容
}

///< summary>
/// UnloadContent将在每场比赛中调用一次,并且是卸载
///全部内容的地方。
///< / summary>
protected override void UnloadContent()
{
// TODO:在此处卸载任何非ContentManager内容
}

///< summary>
///允许游戏运行逻辑,比如更新世界,
///检查冲突,收集输入和播放音频。
///< / summary>
///< param name =gameTime>提供时间值的快照。< / param>
protected override void Update(GameTime gameTime)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState。按下)
this.Exit();

$ b $ // TODO:在这里添加更新逻辑
switch(gameState)
{
case GameStates.Battle:
break;
}
base.Update(gameTime);
}

///< summary>
///当游戏应该绘制时,这被调用。
///< / summary>
///< param name =gameTime>提供时间值的快照。< / param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
$ b $ // TODO:在这里添加绘图代码
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的精灵所在的坐标,纹理的名称),这就是它,但它似乎只是不这样工作。有人知道我可以怎么做吗? 解决方案

它的确如此。将您的精灵特定代码解压缩到类似于以下内容的类中:

  public Sprite()
{

$ b公共无效更新(GameTime gameTime)
{
}

public void Draw(SpriteBatch spriteBatch)
{

$ / code>

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

  Sprite.Update(gameTime); 

  Sprite.Draw(spriteBatch); 

这应该有助于您开始。你是否尝试了一些方法,如果是的话,发布它们,我们可以看到它们出错的地方,或者你只是希望能够快速的复制和粘贴?

I'm trying to program a game with a friend, and I'm very confused as to how to draw sprites. I can draw code in my Game1 class, below.

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

}

In my Game1 code, I can successfully draw that sprite and the text there on the screen. However, I want to draw a sprite in another class, but I have absolutely no idea what to put there.

In the other class, I wish I could just do something as simple as

sprite.Draw(//here would be x and y coords of where the sprite goes, name of the texture), and have that be it, but it seems to just not work that way. Does anyone know how I can go about doing this?

解决方案

It does indeed work like that. Extract your sprite specific code into a class that is structured something similar to:

public Sprite()
{
}

public void Update(GameTime gameTime)
{
}

public void Draw(SpriteBatch spriteBatch)
{
}

Then in your game1 class you can call

Sprite.Update(gameTime); 

and

Sprite.Draw(spriteBatch);

That should help you get started. Did you try some approaches yourself, if so post them and we can see where they went wrong, or are you just hoping for a quick answer you can copy and paste?

这篇关于我如何在自己的课堂中绘制一个精灵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆