在XNA 4中创建新的GameObject(创建新实例)

时间:2012-08-24 21:33:43

标签: c# xna xna-4.0

我有一个用于创建GameObjects的类,它有一个sprite和....来制作一个游戏对象。 我像这样绘制我的纹理:

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之间制作新的GameObject; 请帮帮我。

感谢。

更新: 我试图在游戏运行时添加另一个游戏对象; 并且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是一个列表,它在游戏中有所有gameObjects

1 个答案:

答案 0 :(得分:0)

你试图在游戏运行时添加另一个游戏对象?对不起,您的问题不明确。

我不知道你的Game.gameObjects是一个数组还是一个列表(你可以发布这个类,以及你初始化它的地方吗?)

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

那可能吗?

相关问题