【问题标题】:Make new GameObject in XNA 4(Create new instance)在 XNA 4 中创建新的游戏对象(创建新实例)
【发布时间】:2012-08-25 20:58:27
【问题描述】:

我有一个用于创建游戏对象的类,它获取精灵和....来制作游戏对象。 我这样画我的纹理:

spriteBatch.Begin();
    foreach (GameObject gameObject in Game.gameObjects)
    {
        if (gameObject.visible)
        {
            spriteBatch.Draw(gameObject.sprite.texture, gameObject.rect, gameObject.sprite.sourceRect, gameObject.color, MathHelper.ToRadians(gameObject.rotation), gameObject.sprite.origin, gameObject.spriteEffect, gameObject.layer);
        }
    }
    foreach (Text text in Game.texts)
    {
        spriteBatch.DrawString(text.font, text.text, text.position, text.color, MathHelper.ToRadians(text.rotation), text.origin, text.scale, text.spriteEffects, text.layer);
    }
    spriteBatch.End();

我尝试了一些方法来做到这一点,并在游戏之间创建新对象。例如,当我按下空格时,我想创建新的子弹。 但我不能在游戏之间制作新的游戏对象; 请帮帮我。

谢谢。

更新: 我试图在游戏运行时添加另一个游戏对象; Game.gameObjects 是 Game1 中的静态列表(我将 Game1 名称更改为 Game) 它在 GameObject 类上:

public GameObject(Sprite sprite)
        {
            this.sprite = sprite;
            rectWidth = (int)sprite.texture.Width;
            rectHeight = (int)sprite.texture.Height;
            rect = new Rectangle((int)position.X, (int)position.Y, rectWidth, rectHeight);
            Game.gameObjects.Add(this);
        }

所以我可以让我的游戏对象在游戏中渲染或管理它们; 然后我有 gameObjects 是一个列表,它有游戏中的所有游戏对象

【问题讨论】:

  • 您的问题非常不清楚,您提供的代码似乎与问题无关。您应该重新表述问题,使其更易于理解,否则我们将无法回答您。

标签: c# xna xna-4.0


【解决方案1】:

您试图在游戏运行时添加另一个游戏对象?抱歉,您的问题不清楚。

我不知道你的 Game.gameObjects 是数组还是列表(你能发布这个类吗,你在哪里初始化它?)

Game.gameObjects.Add(new GameObject(Sprite, position, whatever));

这可能吗?

【讨论】:

  • 哦,对不起。我认为它的发生是因为我的语言不是英语,而且我的英语说得不好。我更新了我的第一篇文章。
  • 不要在contructor里面做Game.gameObjects.Add(this);,用它来添加游戏对象,例如如果(SpacePressed) { Game.gameObjects.Add(new GameObject())你做对象的时候不做
  • 非常感谢。我认为我的代码不正确。看到你的评论我就知道了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 2017-09-28
相关资源
最近更新 更多