C# Problem med bugg
Hej
Nu ligger det till så i mitt spel när man skjuter så ökas fiendens hastighet hela tiden, med varje skott.
Detta är väldigt störande då fienderna nästintill flyger fram efter man skjutit några skott. Fiendens hastighet är konstant och borde enligt mig inte ändras heller eller ökas om den nu gör det.
Kod för fiender:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Stickman
{
// Facing directions of the enemies.
enum FaceDirection
{
Left = -1,
Right = 1,
}
class Enemy
{
#region variables
private Level level;
private Vector2 position;
private Rectangle localBounds;
public int health;
// Animations
private Animation runAnimation;
private Animation idleAnimation;
private AnimationPlayer sprite;
// The direction this enemy is facing and moving along the X-axis
private FaceDirection direction = FaceDirection.Left;
// The time the enemy has been waiting before turning around
private float waitTime;
// How long to wait before turning around
private const float MaxWaitTime = 0.5f;
// The speed at which the enemy moves along the X-axis
private const float MoveSpeed = 64.0f;
#endregion
#region properties
public Level Level
{
get { return level; }
}
// Position in the level
public Vector2 Position
{
get { return position; }
}
// The health of the enemy
public int Health
{
get { return health; }
set { health = value; }
}
// Gets a rectangle which bounds this enemy in the level
public Rectangle BoundingRectangle
{
get
{
int left = (int)Math.Round(Position.X - sprite.Origin.X) + localBounds.X;
int top = (int)Math.Round(Position.Y - sprite.Origin.Y) + localBounds.Y;
return new Rectangle(left, top, localBounds.Width, localBounds.Height);
}
}
#endregion
#region Constructors
public Enemy(Level level, Vector2 position, string spriteSet, int _health)
{
this.level = level;
this.position = position;
this.health = _health;
LoadContent(spriteSet);
}
#endregion
#region Methods
// Loads the enemies content and calculate bounds within texture size
public void LoadContent(string spriteSet)
{
// Load Animation
spriteSet = "Sprites/" + spriteSet + "/";
idleAnimation = new Animation(Level.Content.Load<Texture2D>(spriteSet + "idle"), 0.1f, true);
runAnimation = new Animation(Level.Content.Load<Texture2D>(spriteSet + "run"), 0.1f, true);
sprite.PlayAnimation(idleAnimation);
// Calculate bounds within texture size
int width = (int)(idleAnimation.FrameWidth * 0.35);
int left = (idleAnimation.FrameWidth - width) / 2;
int height = (int)(idleAnimation.FrameWidth * 0.7);
int top = idleAnimation.FrameHeight - height;
localBounds = new Rectangle(left, top, width, height);
}
// Updates the enemies so the wait at each end of the platform
public void Update(GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
// Calculate tile position based on the side we are walking towards
float posX = Position.X + localBounds.Width / 2 * (int)direction;
int tileX = (int)Math.Floor(posX / Tile.Width) - (int)direction;
int tileY = (int)Math.Floor(Position.Y / Tile.Height);
if (waitTime > 0)
{
// Wait for some amount of time.
waitTime = Math.Max(0.0f, waitTime - (float)gameTime.ElapsedGameTime.TotalSeconds);
if (waitTime <= 0.0f)
{
// Then turn around
direction = (FaceDirection)(-(int)direction);
}
}
else
{
// If we are about to run into a wall or off a cliff, start waiting
if (Level.GetCollision(tileX + (int)direction, tileY - 1) == TileCollision.Impassable ||
Level.GetCollision(tileX + (int)direction, tileY) == TileCollision.Passable)
{
waitTime = MaxWaitTime;
}
else
{
// Move in the current direction
Vector2 velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f);
position = position + velocity;
}
}
}
// Draws the animated enemy
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
// Stop running when the game is paused or before turning around
if (!Level.Player.IsAlive || Level.ReachedExit ||
Level.TimeRemaining == TimeSpan.Zero || waitTime > 0)
{
sprite.PlayAnimation(idleAnimation);
}
else
{
sprite.PlayAnimation(runAnimation);
}
// Draw facing the way the enemy is moving
SpriteEffects flip = direction > 0 ? SpriteEffects.FlipHorizontally :
SpriteEffects.None;
sprite.Draw(gameTime, spriteBatch, Position, flip);
}
#endregion
}
}
Kod för projectiles:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Stickman
{
/// <summary>
/// This class is a class for different kinds of projectiles.
/// Each projectile will have a weapon which uses the projectiles.
/// </summary>
class Projectile
{
#region variables
private AnimationPlayer sprite;
private Texture2D texture;
private Vector2 position;
float projectileMoveSpeed;
private Player player;
private Rectangle localBounds;
public int damage;
public int Direction;
private bool active;
#endregion
#region properties
public int Damage
{
get { return damage; }
set { damage = value; }
}
// Returns the texture of the projectile
public Texture2D Texture
{
get { return texture; }
}
// Returns the position of the projectile
public Vector2 Position
{
get { return position; }
set { position = value; }
}
public int Width
{
get { return Texture.Width; }
}
public int Height
{
get { return Texture.Height; }
}
public Player Player
{
get { return player; }
}
public bool Active
{
get { return active; }
set { active = value; }
}
public Rectangle BoundingRectangle
{
get
{
int left = (int)Math.Round(Position.X - Texture.Width / 2) + localBounds.X;
int top = (int)Math.Round(Position.Y - Texture.Height / 2) + localBounds.Y;
return new Rectangle(left, top, localBounds.Width, localBounds.Height);
}
}
#endregion
public Projectile(Texture2D texture, Vector2 position, int dmg)
{
this.texture = texture;
this.position = position;
Active = true;
Damage = dmg;
projectileMoveSpeed = 10f;
}
#region Methods
public void Update(GameTime gameTime)
{
position.X += projectileMoveSpeed;
// Set active to false if bullet goes out of bounds
if ((position.X + texture.Width / 2 > 850) || position.X + texture.Width / 2 < 0)
Active = false;
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
//SpriteEffects flip = player.Velocity.X > 0 ? SpriteEffects.FlipHorizontally :
// SpriteEffects.None;
SpriteEffects flip = SpriteEffects.None;
//sprite.Draw(gameTime, spriteBatch, Position, flip);
spriteBatch.Draw(texture, position, null, Color.White, 0f,
new Vector2(Width / 2, Height / 2), 1f, flip, 0f);
}
#endregion
}
}
Sen kanske om någon skulle behöva följande kod
Update för bullets och enemies:
public void UpdateEnemies(GameTime gameTime)
{
for (int i = 0; i < enemies.Count; i++)
{
enemies[i].Update(gameTime);
// Touching an enemy lowers the players health and kills the enemy
if (enemies[i].BoundingRectangle.Intersects(Player.BoundingRectangle))
{
// If a player has 0 lives after the life has been
// subtracted.
if (Player.Lives == 1)
{
// Kill Player
OnPlayerKilled(enemies[i]);
}
else
{
// Else lower players lives with 1
enemies.RemoveAt(i--);
--Player.Lives;
}
}
}
}
public void AddBullet(Vector2 position, Texture2D texture, int dmg)
{
Projectile projectile = new Projectile(texture, position, dmg);
bullets.Add(projectile);
}
public void UpdateBullets(GameTime gameTime)
{
for (int i = 0; i < bullets.Count; i++)
{
// Updates all bullets
bullets[i].Update(gameTime);
for (int e = 0; e < enemies.Count; e++)
{
// Updates all enemies
enemies[e].Update(gameTime);
// If a bullet collides with an enemy
// lower the enemys health and if
// the enemy dies - add 100 to score.
if (bullets[i].BoundingRectangle.Intersects(enemies[e].BoundingRectangle))
{
//enemies[e].Health -= bullets[i].damage;
if (enemies[e].Health <= 0)
{
score += 100;
enemies.RemoveAt(e--);
bullets.RemoveAt(i--);
}
else
{
enemies[e].Health -= bullets[i].damage;
bullets.RemoveAt(i--);
if (enemies[e].Health == 0)
{
score += 100;
enemies.RemoveAt(e--);
}
}
}
}// end enemies for-loop
}// end bullets for-loop
}// end UpdateBullets
Hoppas något ser var felet ligger för jag har tittat efter men lyckas inte lokalisera det