The enemy begins to update at super speed whenever a new one is added to the list, any suggestions?
Wave Manager Class
[code]
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 Shmup
{
class WaveManager
{
public Player LocalPlayer;
public List<Enemy> EnemyList = new List<Enemy>();
float elapsedTime = 0;
ContentManager thePrivateContent;
Enemy LocalEnemy = new Enemy();
public void LoadContent(ContentManager theContent)
{
thePrivateContent = theContent;
LocalEnemy.LoadContent(thePrivateContent);
}
public void Update(GameTime theTime)
{
elapsedTime += theTime.ElapsedGameTime.Milliseconds;
LocalEnemy.WaveType = 1;
LocalEnemy.GunAngle = (float)Math.Atan2(LocalPlayer.position.Y - LocalEnemy.Position.Y,
LocalPlayer.position.X - LocalEnemy.Position.X);
if (elapsedTime >= 500)
{
EnemyList.Add(LocalEnemy);
elapsedTime = 0;
}
foreach (Enemy e in EnemyList)
{
e.Update(theTime);
}
}
public void Draw(SpriteBatch theBatch)
{
foreach (Enemy e in EnemyList)
{
e.Draw(theBatch);
}
}
}
}
[/code]
Enemy
[code]
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 Shmup
{
class Enemy
{
public Rectangle BoundingRectangle;
public List<Bullet> BulletList = new List<Bullet>();
public bool Alive = true;
public Vector2 Position;
public int WaveType;
// 1 - Straight Down
// 2 - Across Left
// 3 - Across Right
// 4 - Diagonal Right
// 5 - Diagonal Left
public float GunAngle;
private float elapsedTime = 0;
private ContentManager thePrivateContent;
Texture2D assetTexture;
public void LoadContent(ContentManager theContent)
{
assetTexture = theContent.Load<Texture2D>("sprites/enemy");
thePrivateContent = theContent;
}
public void Update(GameTime theTime)
{
BoundingRectangle = new Rectangle((int)Position.X, (int)Position.Y, 32, 32);
Vector2 origin = new Vector2(BoundingRectangle.Center.X - 8, BoundingRectangle.Center.Y - 8);
#region Move
if (WaveType == 1)
Position.Y+= 4;
if (WaveType == 2)
Position.X -= 4;
if (WaveType == 3)
Position.X += 4;
if (WaveType == 4)
{
Position.X+= 4;
Position.Y+= 4;
}
if (WaveType == 5)
{
Position.X-= 4;
Position.Y-= 4;
}
#endregion
#region Shoot
elapsedTime += theTime.ElapsedGameTime.Milliseconds;
Bullet bBullet = new Bullet(GunAngle, Color.White);
bBullet.LoadContent(thePrivateContent);
bBullet.Position = origin;
if (Alive)
{
if(elapsedTime >= 50)
{
BulletList.Add(bBullet);
elapsedTime = 0;
}
foreach (Bullet b in BulletList)
{
b.Update(theTime);
}
if (bBullet.Alive == false)
BulletList.Remove(bBullet);
}
#endregion
}
public void Draw(SpriteBatch theBatch)
{
foreach (Bullet b in BulletList)
b.Draw(theBatch);
if (Alive)
theBatch.Draw(assetTexture, BoundingRectangle, Color.White);
}
}
}
[/code]
SingleScreen
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Shmup
{
class SingleScreen:GameScreen
{
Player pPlayer = new Player();
WaveManager wManager = new WaveManager();
public SingleScreen(Game game, SpriteBatch spriteBatch, SpriteFont spriteFont)
: base(game, spriteBatch)
{
wManager.LocalPlayer = pPlayer;
pPlayer.LoadContent(game.Content);
wManager.LoadContent(game.Content);
}
public override void Update(GameTime gameTime)
{
pPlayer.Update(gameTime);
wManager.Update(gameTime);
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
wManager.Draw(spriteBatch);
pPlayer.Draw(spriteBatch);
base.Draw(gameTime);
}
}
}
[/code]
[QUOTE=Loli;23704160][code]
public int WaveType;
// 1 - Straight Down
// 2 - Across Left
// 3 - Across Right
// 4 - Diagonal Right
// 5 - Diagonal Left
[/code][/QUOTE]
[url=http://msdn.microsoft.com/en-us/library/sbbt4032(VS.80).aspx]Enums[/url]
I know, but I'm too lazy to add them in, so far it's mainly testing purposes
You are adding really high numbers every frame. Do the movement like this:
[cpp]Vector2 velocity = Vector2.Zero;
// calculate velocity
position += velocity * gameTime.ElapsedGameTime.TotalSeconds;[/cpp]
Wait... What? I'm simply telling it to add 4 to the position every frame.
[editline]12:44PM[/editline]
I forgot to mention. It's not actually adding anything to the list.
Yeah. If the framerate is 60, you are adding 4 * 60 = 240 to it every second. That's why it's so fast.
It works fine when I don't tell it to add anything new to the list.
[cpp]
Enemy LocalEnemy = new Enemy();
public void Update(GameTime theTime)
{
elapsedTime += theTime.ElapsedGameTime.Milliseconds;
LocalEnemy.WaveType = 1;
LocalEnemy.GunAngle = (float)Math.Atan2(LocalPlayer.position.Y - LocalEnemy.Position.Y,
LocalPlayer.position.X - LocalEnemy.Position.X);
if (elapsedTime >= 500)
{
EnemyList.Add(LocalEnemy);
elapsedTime = 0;
}
foreach (Enemy e in EnemyList)
{
e.Update(theTime);
}
}
[/cpp]
What is LocalEnemy right here? You add this object to the EnemyList every half a second, so after 2 seconds you have 4 references to the same object in EnemyList so the LocalEnemy is being updated 4 times per frame.
OK, It is updating at a totally normal speed now. However it isn't adding anything new to the list any more.
Well that depends on how you've modified your code
[code]
public void Update(GameTime theTime)
{
elapsedTime += theTime.ElapsedGameTime.Milliseconds;
LocalEnemy.WaveType = 1;
LocalEnemy.Update(theTime);
LocalEnemy.GunAngle = (float)Math.Atan2(LocalPlayer.position.Y - LocalEnemy.Position.Y,
LocalPlayer.position.X - LocalEnemy.Position.X);
if (elapsedTime >= 50)
{
EnemyList.Add(LocalEnemy);
elapsedTime = 0;
}
}
[/code]
That's all I've done to change it.
That's worse.. I don't even know what you're trying to do
I'm trying to make my enemy spawn every 0.5 seconds.
As you can see I have set a predefined enemy wave type, gun angle and told it to update.
I've then told the computer to check if it's been 50 milliseconds. if so it adds the Enemy to the list and then the timer is reset to 0.
Okay then you need to create a new instance of Enemy, and then add it to EnemyList
I've already done that. I tell it to add it but it doesn't.
You're only ever acting on LocalEnemy, it looks like you're trying to add the same one every time.
[editline]07:51AM[/editline]
50 miliseconds is 0.05 seconds, not half a second. You want 500 miliseconds there.
Sorry, you need to Log In to post a reply to this thread.