我正在用 XNA 编写我的第一个游戏,我有点困惑。
该游戏是一款 2D 平台游戏,像素完美,不基于 Tiles。
目前,我的代码看起来像这样
public class Game1 : Microsoft.Xna.Framework.Game
{
//some variables
Level currentLevel, level1, level2;
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
//a level contains 3 sprites (background, foreground, collisions)
//and the start position of the player
level1 = new Level(
new Sprite(Content.Load<Texture2D>("level1/intro-1er-plan"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level1/intro-collisions"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level1/intro-decors-fond"), Vector2.Zero),
new Vector2(280, 441));
level2 = new Level(
new Sprite(Content.Load<Texture2D>("level2/intro-1er-plan"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level2/intro-collisions"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level2/intro-decors-fond"), Vector2.Zero),
new Vector2(500, 250));
...//etc
}
protected override void UnloadContent() {}
protected override void Update(GameTime gameTime)
{
if(the_character_entered_zone1())
{
ChangeLevel(level2);
}
//other stuff
}
protected override void Draw(GameTime gameTime)
{
//drawing code
}
private void ChangeLevel(Level _theLevel)
{
currentLevel = _theLevel;
//other stuff
}
每个精灵都是从一开始就加载的,所以这对计算机的 RAM 来说不是一个好主意。
好吧,这是我的问题:
- 我如何用他自己的精灵数量、它们的事件和对象来保存关卡?
- 我如何加载/卸载这些关卡?