Newbie here, I'm creating a Flappy Bird clone using XNA and up until now I'd encountered no unsolvable problems, but I just don't know how to proceed on the spawning of pipes.
I have a pipe class, and I manage to create an instance of it based on a timer in the main game update function (I know this will create the pipe only once):
elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (elapsedTime == 1.0f)
{
Pipe pipe = new Pipe();
}
The problem is that I can't call the pipe.Update() function in the main game update, given that "pipe" hasn't been created yet! And, in any case if I'm to spawn several instances of Pipe.cs, how do I refer to their respective Update() functions?
To create multiple pipes
, you could use a list and simple for
loops (I added a spawntime
factor to make a pipe spawn every 5 seconds to give you an idea of how you could spawn):
List<Pipe> pipes = new List<Pipe>();
int spawnfactor = 5;
int spawns = 0; //or 1, if you want the first spawn to occur at 1 second
protected override void Update()
{
elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (elapsedTime == spawnfactor * spawns)
{
pipes.Add(New Pipe());
spawns++; //keep track of how many spawns we've had
}
for (int i = 0; i < pipes.Count; i++)
{
pipes[i].Update();//update each pipe in the list.
}
}
protected override void Draw()
{
for (int i = 0; i < pipes.Count; i++)
{
pipes[i].Draw();
}
}
Let me know if this helps or if anything's unclear.