0

i've been scanning the web and trying different methods for hours, I tried to put off asking here but I can't see any way around it unfortunately.

My problem is i'm creating a list of objects, birds specifically. And in my Initialisation method I have a for loop that runs through 10 times creating a new bird object, however when I run the game, it only creates one. I've looked through my code over and over and I can't seem to find the issue, something tells me when I fix it i'll kick myself for it :).

Thanks In Advance, Joe

Code:

Game1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlummetGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D backgroundTexture;

        // Game Objects
        Plane gamePlane;
        Truck gameTruck;
        Player playerOne;

        int maxBirds = 10;
        List<Bird> birdList = new List<Bird>();

        public bool playerIsActive;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Screen resolution of 1280x720
            graphics.PreferredBackBufferHeight = 720;
            graphics.PreferredBackBufferWidth = 1280;
        }

        protected override void Initialize()
        {
            // Game Object Init
            gamePlane = new Plane();
            gameTruck = new Truck();
            Plane.gfxViewport = GraphicsDevice.Viewport;

            for (int i = 0; i < maxBirds; i++)
            {
                birdList.Add(new Bird());
            }

            playerIsActive = false;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Game Object Sprite Loading
            Truck.Texture = Content.Load<Texture2D>("truck");
            Plane.Texture = Content.Load<Texture2D>("plane");
            Player.Texture = Content.Load<Texture2D>("parachutes");
            Bird.Texture = Content.Load<Texture2D>("BirdFlying");
            Truck.myFont = Content.Load<SpriteFont>("myFont");

            backgroundTexture = Content.Load<Texture2D>("background");

        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            gamePlane.Update();
            gameTruck.Update();

            if(Keyboard.GetState().IsKeyDown(Keys.Back) || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {this.Exit();}

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                playerOne = new Player(new Vector2(gamePlane.Position.X, gamePlane.Position.Y + 25));
                playerIsActive = true;
            }

            if (playerIsActive == true)
            {
                playerOne.Update();
            }

            foreach (Bird ID in birdList)
            {
                ID.Update(gameTime);
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {

            spriteBatch.Begin();
            // Must come first, background.
            DrawScenery();

            gamePlane.Draw(spriteBatch);
            gameTruck.Draw(spriteBatch);
            if (playerIsActive)
            {
                playerOne.Draw(spriteBatch);
            }

            foreach (Bird ID in birdList)
            {
                ID.Draw(spriteBatch);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void DrawScenery()
        {
            Rectangle screenRectangle = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
            spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
        }
    }
}

Bird.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlummetGame
{
    public class Bird
    {
        public enum Directions { Left, Right };

        // Static Fields
        public static Texture2D Texture;

        public static Directions Direction;

        public static Viewport gfxViewport;

        public int currentFrame = 0;
        private const int Frames = 9;
        private const int FramesPerSec = 2;

        // Fields 
        public Vector2 Position;

        // Declaring & Initialising a random object.
        Random r = new Random();

        SpriteEffects flip = SpriteEffects.FlipHorizontally;

        //Constructor
        public Bird()
        {
            int tempval;
            // Set the initial position of the bird.
            Position = new Vector2(r.Next(10, 1270), r.Next(10, 710));

            // Decides inital direction.
            tempval = r.Next(1, 2);
            if (tempval == 1)
            { Direction = Directions.Left; }
            else
            { Direction = Directions.Right; }

        }

        public void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            // Boundary Checking * No need to check for Y as bird only moves laterally
            if (Position.X < 0)
            { Direction = Directions.Right; }

            if (Position.X > gfxViewport.Width - Texture.Width)
            { Direction = Directions.Left; }

            // Movement Handling
            if (Direction == Directions.Left)
            {
                flip = SpriteEffects.FlipHorizontally;
                Position.X--;
            }

            if (Direction == Directions.Right)
            {
                Position.X++;
                flip = SpriteEffects.None;
            }

            // Frame Animation
            currentFrame = (int)elapsed;

            if (currentFrame > Frames) { elapsed = 0; }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, 35, 41), new Rectangle(39, 0, 38, 65), Color.White);
        }
    }
}
Martin
  • 2,411
  • 11
  • 28
  • 30
  • 1
    Are you sure its creating one, or 10 in exactly the same position? Random really isn't random, you need to seed it and make it static for it to position it randomly. – Ron Beyer May 12 '15 at 02:26
  • can you step through the code and try to pinpoint where the issue is happening.. if you know where it's happening.. tell us what line it's happening on please – MethodMan May 12 '15 at 02:31
  • Hi, see the question I linked as a duplicate. You need to have one global instance of Random (Or pass it as an argument to your constructor). Creating a new Random in a loop will always yield the same number as it's based on the timestamp and the loop goes too quickly. You have 10 birds in the same spot. – Pierre-Luc Pineault May 12 '15 at 03:09
  • Yes, you're correct, thank you alot. Told you I'd kick myself once i heard this ! – Joe Smith May 12 '15 at 06:56

0 Answers0