I’m doing my Lab and stuck with one thing.
This is my Lab problem.
"Problem 3 – Spawning a new teddy bear.
Add code to replace the current teddy bear with a newly-spawned teddy bear every time the Left Mouse button is pressed. The behavior you’ll see is that the teddy bear will appear to jump when the A button is pressed; this happens because the teddy bear variable now refers to the new teddy bear object and the old teddy bear object is now garbage to be collected.
Add code to check if the Right Mouse button is pressed. If it is, start playing the explosion at the center of the teddy bear and replace the current teddy bear with a newly-spawned teddy bear. To spawn a new teddy bear, your new code needs to:
Generate a random x for the new teddy bear
Generate a random y for the new teddy bear
Generate a random velocity x for the new teddy bear
Generate a random velocity y for the new teddy bear
Call the TeddyBear constructor to put a new TeddyBear object into your teddy bear variable
CAUTION: Making the Left Mouse and Right Mouse buttons work properly is harder than you may think! You only want to respond to the FIRST time a button is pressed then wait until it’s been released before you respond to it again. You’ll have to figure out how to make it work that way. Hint: keeping track of the previous state of each button will help you make this all work properly."
That is my code
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ExplodingTeddies;
namespace Lab11
{
///
/// This is the main type for your game.
///
public class Game1 : Game
{
public const int WindowWidth = 800;
public const int WindowHeight = 600;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// declare contants
// declare variables for bear and explosion objects
TeddyBear bear0;
TeddyBear bear1;
Explosion boom;
// random support
System.Random rand = new System.Random(1);
System.Random rand1 = new System.Random();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = WindowWidth;
graphics.PreferredBackBufferHeight = WindowHeight;
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);
// Create bear object
float bearSpeedX = (float)rand.NextDouble();
float bearspeedY = (float)rand.NextDouble();
Vector2 bearVelocity = new Vector2(bearSpeedX, bearspeedY);
bear0 = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics\teddybear", WindowWidth / 2, WindowHeight / 2, bearVelocity);
int randomX = rand1.Next(0, WindowWidth);
int randomY = rand1.Next(0, WindowHeight);
float randomVelocityX = (float)rand.NextDouble();
float randomVelocityY = (float)rand.NextDouble();
Vector2 bear1Velocity = new Vector2(randomVelocityX, randomVelocityY);
bear1 = new TeddyBear(Content, WindowWidth, WindowHeight,
@"graphics\teddybear", randomX, randomY,
bear1Velocity);
// create explosion
boom = new Explosion(Content, @"graphics\explosion");
}
/// <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();
// Update oblects
bear0.Update(gameTime);
boom.Update(gameTime);
MouseState mouse = Mouse.GetState();
if (mouse.LeftButton == ButtonState.Pressed && bear0.Active == true || mouse.RightButton == ButtonState.Pressed && bear0.Active == true)
{
bear0.Active = false;
boom.Play(bear0.DrawRectangle.X, bear0.DrawRectangle.Y);
}
if (mouse.RightButton == ButtonState.Released && bear0.Active == false)
{
bear1.Update(gameTime);
}
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.CornflowerBlue);
// TODO: Draw bear and explosion
spriteBatch.Begin();
bear0.Draw(spriteBatch);
if (bear0.Active == false)
{
bear1.Draw(spriteBatch);
}
boom.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
And this is TeddyBear class that mentor provided.
public class TeddyBear
{
public TeddyBear(ContentManager contentManager, int windowWidth, int windowHeight, string spriteName, int x, int y);
public TeddyBear(ContentManager contentManager, int windowWidth, int windowHeight, string spriteName, int x, int y, Vector2 velocity);
public bool Active { get; set; }
public Rectangle DrawRectangle { get; }
public void Draw(SpriteBatch spriteBatch);
public void Update(GameTime gameTime);
}
And Explosion Class that mentor provided.
namespace ExplodingTeddies
public class Explosion
{
public Explosion(ContentManager contentManager, string stripName);
public void Draw(SpriteBatch spriteBatch);
public void Play(int x, int y);
public void Update(GameTime gameTime);
}
My problem is that even I press Left Button or Right button: result the same - bear0 plays Explosion animation and spawn bear 1. But I need to spawn new bear only after Right Button click!
My second problem, that I need to re-spawn more than 1 bear, and I tried few different ways, and it doesn’t work well, so I really need help.