0

I've seen questions similar to what I'm asking, but they don't quite give me the answer I'm looking for. I want 5 instances of my sprites to show up on screen and spawn in different locations. For some reason though instead of 5 instances, each "instance" of the sprite increases the values of the one sprite that does appear on screen.

Sprite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Lab05_2__Aaron_Benjamin_
{
class Sprite
{
    Texture2D mSpriteTexture;
    public Vector2 Position;
    Color mSpriteColor;
    public Rectangle Size;
    public string AssetName;
    private float mScale = 1.0f;

    public float Scale 
    {
        get { return mScale; }
        set
        {
            mScale = value;
            //Recalculate the Size of the Sprite with the new scale
            Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
        }
    }

    public void LoadContent(ContentManager theContentManager, string theAssetName)
    {
        mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
        AssetName = theAssetName;
        Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
    }

    public  void Update(GameTime gameTime)
    {


    }

    public void Draw(SpriteBatch theSpriteBatch)
    {
         theSpriteBatch.Draw(mSpriteTexture, Position,
            new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
             Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);

        //theSpriteBatch.Draw(mSpriteTexture, Position);
    }
  }
}

Enemy.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;

namespace Lab05_2__Aaron_Benjamin_
{
    class Enemy : Sprite
{
    const string ENEMY_ASSETNAME = "gear";
    Random Rand = new Random();
    int startPos_X;
    int startPos_Y;

    public void LoadContent(ContentManager theContentManager)
    {
        Position = new Vector2(startPos_X = Rand.Next(0 , 1000), startPos_Y = Rand.Next(0, 1000)); //= Rand.Next(0, 100),Position.Y = Rand.Next(0,100));

        base.LoadContent(theContentManager, ENEMY_ASSETNAME);
    }

    public void Update(GameTime gameTime)
    {
        MouseState cState = Mouse.GetState();


        if (Position.X > cState.X)
        {
            Position.X += 1;
        }
        if (Position.X < cState.X)
        {
            Position.X -= 1;
        }
        if (Position.Y < cState.Y)
        {
            Position.Y -= 1;
        }
        if (Position.Y > cState.Y)
        {
            Position.Y += 1;
        }

        base.Update(gameTime);
       }        
     }
   }

Game1.cs

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Lab05_2__Aaron_Benjamin_
{
/// <summary>
/// This is the main type for your game.
/// </summary>

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    List<Enemy> enemies = new List<Enemy>();




    private void CreateEnemy()
    {
        Enemy gear = new Enemy();
        Random rand = new Random();
        //gear = new Enemy();
        //gear.Position.X = rand.Next(0, 1000);
        //gear.Position.Y = rand.Next(0, 1000);
        Console.WriteLine(gear.Position.Y);
        enemies.Add(gear);

    }

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

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here


        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        for (int i = 0; i < 5; i++)
        {
            CreateEnemy();
        }

        foreach (var cog in enemies)
        {
            cog.LoadContent(this.Content);
        }
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// game-specific content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        // TODO: Add your update logic here
        foreach (var cog in enemies)
        {
            if (cog.Position.X > Window.ClientBounds.Width - 50)
                cog.Position.X = Window.ClientBounds.Width - 50;
            if (cog.Position.Y > Window.ClientBounds.Height - 50)
                cog.Position.Y = Window.ClientBounds.Height - 50;

            if (cog.Position.X < 0)
                cog.Position.X = 0;
            if (cog.Position.Y < 0)
                cog.Position.Y = 0;
        }

        foreach (var cog in enemies)
        {
            cog.Update(gameTime);
        }

        if (Keyboard.GetState().IsKeyDown(Keys.M))
        {
            // If 'm' is down, we create a new meteor. Note that once this is working
            // this is going to make a lot of meteors. That's another issue, though.
            CreateEnemy();
        }
        //Console.WriteLine(enemies.Count);
        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        foreach (var cog in enemies)
        {
           cog.Draw(spriteBatch);
        }
        spriteBatch.End();
        base.Draw(gameTime);
      }
   }
}
Starkium
  • 1
  • 2

2 Answers2

0

Shouldn't you be using gear in this part instead of this.gear ?

 foreach (var gear in enemies)
 {
       this.gear.LoadContent(this.Content);
 }
  • my friend just noticed that. I changed all those to cog instead of gear and moved where the instance is created. It's still only one sprite showing up, but with the correct speed. Something tells me that the random position isn't a new random for each sprite. I'll update the code. – Starkium Mar 19 '16 at 05:00
0

ok I got it to work. So my randoms were being assigned and then reassigned in a different section of the code so that they all stacked on top of each other. Here is the solution.

Sprite.cs

class Sprite
{
    Texture2D mSpriteTexture;
    public Vector2 Position;
    Color mSpriteColor;
    public Rectangle Size;
    public string AssetName;
    private float mScale = 1.0f;

    public float Scale 
    {
        get { return mScale; }
        set
        {
            mScale = value;
            //Recalculate the Size of the Sprite with the new scale
            Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
        }
    }

    public void LoadContent(ContentManager theContentManager, string theAssetName)
    {
        mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
        AssetName = theAssetName;
        Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
    }

    public  void Update(GameTime gameTime)
    {


    }

    public void Draw(SpriteBatch theSpriteBatch)
    {
         theSpriteBatch.Draw(mSpriteTexture, Position,
            new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
             Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);

        //theSpriteBatch.Draw(mSpriteTexture, Position);
    }

   }
}

Enemy.cs

class Enemy : Sprite
{
    const string ENEMY_ASSETNAME = "gear";
    Random Rand = new Random();
    //int startPos_X;
    //int startPos_Y;

    public void LoadContent(ContentManager theContentManager)
    {
        Position = new Vector2(Position.X ,Position.Y);

        base.LoadContent(theContentManager, ENEMY_ASSETNAME);
    }

    public void Update(GameTime gameTime)
    {
        MouseState cState = Mouse.GetState();


        if (Position.X > cState.X)
        {
            Position.X += 1;
        }
        if (Position.X < cState.X)
        {
            Position.X -= 1;
        }
        if (Position.Y < cState.Y)
        {
            Position.Y -= 1;
        }
        if (Position.Y > cState.Y)
        {
            Position.Y += 1;
        }

        base.Update(gameTime);
    }        
  }
}

Game1.cs

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    List<Enemy> enemies = new List<Enemy>();
    Random rand = new Random();



    private void CreateEnemy()
    {
        Enemy gear = new Enemy();

        //gear = new Enemy();
        gear.Position.X = rand.Next(0, 1000);
        gear.Position.Y = rand.Next(0, 1000);
        Console.WriteLine(gear.Position.Y);
        enemies.Add(gear);
        enemies[0].Position.X += 10;

    }

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

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here


        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        for (int i = 0; i < 5; i++)
        {
            CreateEnemy();
        }

        foreach (var cog in enemies)
        {
            cog.LoadContent(this.Content);
        }
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// game-specific content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        // TODO: Add your update logic here
        foreach (var cog in enemies)
        {
            if (cog.Position.X > Window.ClientBounds.Width - 50)
                cog.Position.X = Window.ClientBounds.Width - 50;
            if (cog.Position.Y > Window.ClientBounds.Height - 50)
                cog.Position.Y = Window.ClientBounds.Height - 50;

            if (cog.Position.X < 0)
                cog.Position.X = 0;
            if (cog.Position.Y < 0)
                cog.Position.Y = 0;
        }

        foreach (var cog in enemies)
        {
            cog.Update(gameTime);
        }

        if (Keyboard.GetState().IsKeyDown(Keys.M))
        {
            // If 'm' is down, we create a new meteor. Note that once this is working
            // this is going to make a lot of meteors. That's another issue, though.
            CreateEnemy();
        }
        //Console.WriteLine(enemies.Count);
        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        foreach (var cog in enemies)
        {
           cog.Draw(spriteBatch);
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
  }
}
Starkium
  • 1
  • 2