Format Exception - c#

The below snipets are from my Form1 code. I keep getting a Format Exception because of my MessageBox.Show(…); declaration in my Stop() method. Why? What am I doing wrong?
...
private TimeSpan iterationDuration = TimeSpan.Zero;
...
public void Stop()
{
IsGameOver = true;
MessageBox.Show(String.Format("Game Over\n\nScore = {0}\n\n Time Duration = {l}", score, iterationDuration));
Application.Exit();
}
public void Start()
{
score = 0;
IsGameOver = false;
currentRedLightX = 0;
currentRedLightY = 0;
currentGreenLightX = width / 2;
currentGreenLightY = height / 2;
double minIterationDuration = SPEED; // 50 frames / sec
//game loop
while (!IsGameOver)
{
if (IsCollision())
{
score += 10;
}
DateTime startIterationTime = System.DateTime.Now;
UpdateGameState();
Render();
DateTime endIterationTime = System.DateTime.Now;
TimeSpan iterationDuration = endIterationTime - startIterationTime;
if (iterationDuration.TotalMilliseconds < minIterationDuration)
Thread.Sleep(Convert.ToInt32(minIterationDuration - iterationDuration.TotalMilliseconds));
Application.DoEvents();
}
}

That's an {l} (lower case L) not a 1...

Related

C# OutOfRangeException that should be impossible

I am so confessed right now I don't even know how to properly form this question.
I have some code (as shown below) that is run on a different thread with the variable i not being referenced anywhere else that could interfere with it here. I just don't understand what is happening to cause this error, I put some watchers down with visual studio code and the values all seem to be fine and in range but almost randomly out of nowhere I will get this error.
Is it possible that this is caused by another section of code despite to my best knowledge being completely isolated from it? I even went as far to rename all other uses of i to different letters and I still get this problem.
Is it just something with for loops?
Am I somehow modifying i without even know it?
I just don't even know what to ask.
Here is the full code
`
using SFML.Graphics;
using SFML.Window;
using System.Diagnostics;
using System.Numerics;
namespace RenderEngine
{
public class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
PhysicsEngine re = new PhysicsEngine();
for (int i = 0; i < 100; i++)
{
Debug.WriteLine(i);
}
}
}
public class PhysicsEngine
{
Solver solver = new Solver();
public void RenderEngine2()
{
ContextSettings settings = new ContextSettings();
settings.AntialiasingLevel = 8;
RenderWindow window = new RenderWindow(new VideoMode(2000, 1000), "Poop", Styles.Default, settings);
SFML.Graphics.Color color = new SFML.Graphics.Color(0, 0, 0, 0);
window.SetVerticalSyncEnabled(true);
CircleShape shape = new CircleShape(5);
shape.FillColor = new SFML.Graphics.Color(0, 0, 255, 255);
window.Closed += handelclose;
int drawcount = 0;
while (window.IsOpen)
{
window.Clear(color);
window.DispatchEvents();
try
{
foreach (Partical partical in solver.Particals)
{
shape.Position = new SFML.System.Vector2f((partical.position.X), (partical.position.Y));
window.Draw(shape);
drawcount++;
}
drawcount = 0;
}
catch
{
Debug.WriteLine("notready yet");
}
window.Display();
}
void handelclose(object sender, EventArgs e)
{
window.Close();
Environment.Exit(Environment.ExitCode);
}
}
List<Partical> todraw = new List<Partical>();
void EngineLoop()
{
while (true)
{
foreach (Partical partical in solver.Particals)
{
int x = (int)Math.Round(partical.position.X/10);
int y = (int)Math.Round(partical.position.Y/10);
List<int> ts = solver.Grid[x, y];
if (ts != null)
{
for (int brokenint = 0; brokenint < ts.Count; brokenint++)
{
Debug.WriteLine(partical.ID);
Debug.WriteLine(ts[brokenint]);
if (partical.ID != ts[brokenint])
{
Vector2 pos = new Vector2(partical.ID, ts[brokenint]);
if (solver.Collision.Count > 0)
{
if (!solver.Collision.Contains(pos))
solver.Collision.Add(pos);
}
else
{
solver.Collision.Add(pos);
}
}
}
}
}
}
}
private int particalcount = 10;
bool canstart = false;
public PhysicsEngine()
{
Parallel.Invoke(() =>
{
while (!canstart) { Thread.Sleep(100); }
Debug.WriteLine("third thread");
RenderEngine2();
},
() =>
{
while (!canstart) { Thread.Sleep(100); }
Debug.WriteLine("engine started");
EngineLoop();
},
() =>
{
Debug.WriteLine("first thread");
PhysicsLoop(this);
}
);
}
void PhysicsLoop(PhysicsEngine PhyEng)
{
int frames = 0;
long second = 0;
PhysicsStart(PhyEng);
Thread.Sleep(1000);
Stopwatch sw = Stopwatch.StartNew();
solver.startupdate();
while (true)
{
sw.Start();
todraw = solver.Particals;
solver.update();
frames++;
if (second != (Stopwatch.GetTimestamp() / Stopwatch.Frequency))
{
second = (Stopwatch.GetTimestamp() / Stopwatch.Frequency);
Debug.WriteLine(frames);
frames = 0;
}
sw.Stop();
sw.Reset();
if (sw.ElapsedMilliseconds < 15)
Thread.Sleep(15 - (int)sw.ElapsedMilliseconds);
}
}
void PhysicsStart(PhysicsEngine phyeng)
{
for (int i = 0; i < 210; i++)
{
for (int j = 0; j < 110; j++)
{
solver.Grid[i,j] = new List<int>();
}
}
Random rand = new Random();
for (int i = 0; i < particalcount; i++)
{
Partical partical = new Partical();
partical.position = new Vector2(rand.Next(0, 2000), rand.Next(0, 1000));
partical.oldposition = partical.position;
partical.ID = i;
int x1 = (int)Math.Round((partical.position.X + 5) / 10);
int y1 = (int)Math.Round((partical.position.Y + 5) / 10);
int x2 = (int)Math.Round((partical.position.X - 5) / 10);
int y2 = (int)Math.Round((partical.position.Y - 5) / 10);
solver.Grid[x1, y1].Add(partical.ID);
solver.Grid[x2, y1].Add(partical.ID);
solver.Grid[x1, y2].Add(partical.ID);
solver.Grid[x2, y2].Add(partical.ID);
solver.Particals.Add(partical);
}
canstart = true;
}
}
public class Partical
{
public Vector2 position = new Vector2(0, 0);
public Vector2 oldposition = new Vector2(0, 0);
public Vector2 acceleration = new Vector2(0, 0);
Vector2 zero = new Vector2(0, 0);
public int ID = new int();
public void updatePosition(float sub)
{
Vector2 velocity = position - oldposition;
oldposition = position;
position = position + (velocity * 0.9f) + acceleration * sub;
acceleration = zero;
}
public void accelerate(Vector2 accel)
{
acceleration = acceleration + accel;
}
}
public class Solver
{
public List<Partical> Particals = new List<Partical>();
public List<Vector2> Collision = new List<Vector2>();
public List<int>[,] Grid = new List<int>[2100,1100];
public void update()
{
int subcount = 8;
float sub = 1f / (float)subcount;
for (int i = 0; i < subcount; i++)
{
applyGravity(sub);
updatePositions(sub);
for (int j = 0; j < Collision.Count; j++)
{
solvecolisions((int)Collision[j].X, (int)Collision[j].Y);
}
Collision.Clear();
}
}
public void startupdate()
{
applyGravity(0.5f);
updatePositions(0.5f);
applyGravity(0.5f);
updatePositions(0.5f);
}
void updatePositions(float sub)
{
foreach (Partical partical in Particals)
{
partical.updatePosition(sub);
int x1 = (int)Math.Round((partical.oldposition.X + 5) / 10);
int y1 = (int)Math.Round((partical.oldposition.Y + 5) / 10);
int x2 = (int)Math.Round((partical.oldposition.X - 5) / 10);
int y2 = (int)Math.Round((partical.oldposition.Y - 5) / 10);
Grid[x1,y1].Remove(partical.ID);
Grid[x2,y1].Remove(partical.ID);
Grid[x1,y2].Remove(partical.ID);
Grid[x2,y2].Remove(partical.ID);
x1 = (int)Math.Round((partical.position.X + 5) / 10);
y1 = (int)Math.Round((partical.position.Y + 5) / 10);
x2 = (int)Math.Round((partical.position.X - 5) / 10);
y2 = (int)Math.Round((partical.position.Y - 5) / 10);
Grid[x1,y1].Add(partical.ID);
Grid[x2,y1].Add(partical.ID);
Grid[x1,y2].Add(partical.ID);
Grid[x2,y2].Add(partical.ID);
}
}
void applyGravity(float sub)
{
float gravitystrangth = -0.1f;
foreach (Partical partical in Particals)
{
float a = partical.position.Y;
float b = partical.position.X;
b -= 1000;
a -= 500;
double angle = Math.Atan2(a, b);
float newA = gravitystrangth * (float)(Math.Sin(angle));
float newB = gravitystrangth * (float)(Math.Sin((Math.PI / 180) * 90 - angle));
Vector2 gravity = new Vector2(newB, newA);
partical.accelerate(gravity);
}
}
void solvecolisions(int id1, int id2)
{
Partical part = Particals[id1];
Partical part2 = Particals[id2];
if (part != part2)
{
Vector2 colisionaxis = part.position - part2.position;
float dist = colisionaxis.Length();
if (dist < 10)
{
Vector2 n = colisionaxis / dist;
float delta = 10 - dist;
part.position += 0.5f * delta * n;
part2.position -= 0.5f * delta * n;
}
}
}
public List<Partical> GetParticals()
{
return Particals;
}
}
}
`
You ts list depends on solver.Grid[x,y]. I am sure that some of your code that is not visible on the screen makes some changes of solver.Grid, probably deletes some times, or replaces them. This is what causing the error.

Repeating countdown timer unity

I'm new to unity. My intention is to make a countdown timer start with 5 seconds and after it reached to zero I need to restart again from 5 repeatedly. Help me with this thank you.
my current code:
private bool stopTimer;
[HideInInspector]public float time1;
private float deltaT;
void Start()
{
stopTimer = false;
timerSlider.maxValue = gameTime;
timerSlider.value = gameTime;
}
// Update is called once per frame
public void Update()
{
deltaT = Time.time;
timerR();
}
public void timerR()
{
time1 = gameTime - deltaT;
Debug.Log(time1);
int minutes = Mathf.FloorToInt(time1 / 60);
int seconds = Mathf.FloorToInt(time1 - minutes * 60f);
string textTime = string.Format("{0:0}:{1:00}", minutes, seconds);
if (time1 <= 0)
{
stopTimer = true;
}
if (stopTimer == false)
{
timerText.text = textTime;
timerSlider.value = time1;
}
}
}
You can use the power of Coroutine and some handy C# classes like TimeSpan and Stopwatch, something like below code.
using System;
using System.Collections;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;
public class CountDown : MonoBehaviour
{
public Text text;
public int secounds = 5;
public int waitSecOnEachRound = 1;
readonly Stopwatch timer = new Stopwatch();
void Start()
{
StartCoroutine(CounDowntStarter());
}
IEnumerator CounDowntStarter()
{
TimeSpan timeStart = TimeSpan.FromSeconds(secounds);
while (true)
{
timer.Restart();
while (timer.Elapsed.TotalSeconds <= secounds)
{
yield return new WaitForSeconds(0.01f);
timeStart = TimeSpan.FromSeconds(secounds - Math.Floor(timer.Elapsed.TotalSeconds));
text.text = string.Format("{0:00} : {1:00}", timeStart.Minutes, timeStart.Seconds);
}
yield return new WaitForSeconds(waitSecOnEachRound);
}
}
}
In this web you have a simple timer:
https://answers.unity.com/questions/351420/simple-timer-1.html
Here my example code:
float actualtime=0; #Time in the coundownt that we have now
public float maxtime=5;
float actualtimedelta=0;
string texTime
void Update()
{
actualtime=Time.deltaTime-actualtimedelta
if(actualtime>=5)
{
actualtimedelta=Time.deltatime
textTime ="00:00";
}
else
{
actualtime= maxtime-actualtime;
int seconds= Mathf.FloorToInt(actualtime);
int miliseconds=Mathf.FloorToInt((actualtime -seconds)*100)
texTime= string.Format("{0:0}:{1:00}", seconds, miliseconds);
}
}

Trying to figure out how to pause and resume in Cocossharp (BouncyGame)

So I'm playing around with the BouncyGame. I made it so that when you start the game you need to press the screen for it to start. I would like to implement this whenever you play a new round as well. I tried to reuse this att the bottom of my code but it made it extremely laggy.
// Register for touch events
var touchListener = new CCEventListenerTouchAllAtOnce();
touchListener.OnTouchesEnded = OnTouchesEnded;
touchListener.OnTouchesMoved = OnTouchesEnded;
AddEventListener(touchListener, this);
}
void OnTouchesEnded(List<CCTouch> touches, CCEvent touchEvent)
{
if (touches.Count > 0)
{
Schedule(RunGameLogic);
scoreLabel.Text = "Score: 0";
paddleSprite.RunAction(new CCMoveTo(.1f, new CCPoint(touches[0].Location.X, paddleSprite.PositionY)));
}
}
I have no idea how to do this, tried for 2 hours with 0 results. Any suggestions are welcome.
Here's the full code.
using System;
using System.Collections.Generic;
using CocosSharp;
using Microsoft.Xna.Framework;
namespace CocosSharpGameTest
{
public class IntroLayer : CCLayerColor
{
// Define a label variable
CCLabel scoreLabel;
CCSprite paddleSprite, ballSprite;
public IntroLayer() : base(CCColor4B.Black)
{
// create and initialize a Label
scoreLabel = new CCLabel("Tap to GO!", "Arial", 80, CCLabelFormat.SystemFont);
// add the label as a child to this Layer
scoreLabel.PositionX = 50;
scoreLabel.PositionY = 1000;
scoreLabel.AnchorPoint = CCPoint.AnchorUpperLeft;
AddChild(scoreLabel);
paddleSprite = new CCSprite("paddle.png");
AddChild(paddleSprite);
ballSprite = new CCSprite("ball.png");
AddChild(ballSprite);
}
protected override void AddedToScene()
{
base.AddedToScene();
// Use the bounds to layout the positioning of our drawable assets
CCRect bounds = VisibleBoundsWorldspace;
// position the label on the center of the screen
paddleSprite.PositionX = 100;
paddleSprite.PositionY = 100;
ballSprite.PositionX = 320;
ballSprite.PositionY = 640;
// Register for touch events
var touchListener = new CCEventListenerTouchAllAtOnce();
touchListener.OnTouchesEnded = OnTouchesEnded;
touchListener.OnTouchesMoved = OnTouchesEnded;
AddEventListener(touchListener, this);
}
void OnTouchesEnded(List<CCTouch> touches, CCEvent touchEvent)
{
if (touches.Count > 0)
{
Schedule(RunGameLogic);
scoreLabel.Text = "Score: 0";
paddleSprite.RunAction(new CCMoveTo(.1f, new CCPoint(touches[0].Location.X, paddleSprite.PositionY)));
}
}
float ballXVelocity;
float ballYVelocity;
// How much to modify the ball's y velocity per second:
const float gravity = 140;
int score = 0;
void RunGameLogic(float frameTimeInSeconds)
{
// This is a linear approximation, so not 100% accurate
ballYVelocity += frameTimeInSeconds * -gravity;
ballSprite.PositionX += ballXVelocity * frameTimeInSeconds;
ballSprite.PositionY += ballYVelocity * frameTimeInSeconds;
bool overlap = ballSprite.BoundingBoxTransformedToParent.IntersectsRect(paddleSprite.BoundingBoxTransformedToParent);
bool movingDown = ballYVelocity < 0;
if (overlap && movingDown)
{
ballYVelocity *= -1;
const float minXVelocity = -300;
const float maxXVelocity = 300;
ballXVelocity = CCRandom.GetRandomFloat(minXVelocity, maxXVelocity);
score++;
scoreLabel.Text = "Score: " + score;
}
float ballRight = ballSprite.BoundingBoxTransformedToParent.MaxX;
float ballLeft = ballSprite.BoundingBoxTransformedToParent.MinX;
float screenRight = VisibleBoundsWorldspace.MaxX;
float screenLeft = VisibleBoundsWorldspace.MinX;
bool shouldReflectXVelocity =
(ballRight > screenRight && ballXVelocity > 0) ||
(ballLeft < screenLeft && ballXVelocity < 0);
if (shouldReflectXVelocity)
{
ballXVelocity *= -1;
}
if (ballSprite.PositionY < VisibleBoundsWorldspace.MinY)
{
ballSprite.PositionX = 320;
ballSprite.PositionY = 640;
ballXVelocity = 0;
ballYVelocity = 0;
ballYVelocity *= -1;
scoreLabel.Text = "Score: 0";
score = 0;
}
}
}
}
Thanks in advance!
Figured it out!
There is an "Unschedule" Method built into Cocossharp.
Ref. https://developer.xamarin.com/api/namespace/CocosSharp/
I just added
Unschedule(RunGameLogic);
at the very en of my RunGameLogic method under
if (ballSprite.PositionY < VisibleBoundsWorldspace.MinY)
So once the ballSprite is out of bounds it will Unschedule what i Scheduled in my OntouchesEnded method. That means the code goes back to listening for touches.
Might have made some errors, but this is as best I could figure it out and it works!

C# Delta Time Implementation

Heres a code snippet from my attempt to make a 2D particle sim
static long lastTime = 0;
static double GetDeltaTime()
{
long now = DateTime.Now.Millisecond;
double dT = (now - lastTime); // / 1000
lastTime = now;
Console.WriteLine(dT);
return dT;
}
It should be pretty obvious that it would return the time (in milliseconds) since the last time that method was called. Only problem, this is what it prints
393
1
0
0
0
0
0
0
0
0
0
...
Ok so maybe thats just because each pass is taking less than a millisecond. So i changed it to
static long lastTime = 0;
static double GetDeltaTime()
{
long now = DateTime.Now.Ticks; // Changed this to ticks
double dT = (now - lastTime); // / 1000
lastTime = now;
Console.WriteLine(dT);
return dT;
}
but that still prints
6.35476136625848E+17
20023
0
0
0
0
0
0
...
and if "particle simulator" isnt a good enough indicator of how complex my program is, let me just say, it takes a lot longer than 0 ticks to complete a pass!
So whats going on here?
------- Code Reference ------
Heres the whole class just for reference
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace _2D_Particle_Sim
{
static class Program
{
public static Particle2DSim pSim;
static Form1 form;
public static Thread update = new Thread(new ThreadStart(Update));
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
form = new Form1();
pSim = new Particle2DSim(form);
pSim.AddParticle(new Vector2(-80, -7), 5);
pSim.AddParticle(new Vector2(8, 7), 3);
Console.WriteLine("Opening Thread");
Program.update.Start();
Application.Run(form);
// System.Threading.Timer timer;
// timer = new System.Threading.Timer(new TimerCallback(Update), null, 0, 30);
}
static void Update()
{
GetDeltaTime();
while (true)
{
pSim.Update(GetDeltaTime());
}
}
static long lastTime = 0;
static double GetDeltaTime()
{
long now = DateTime.Now.Ticks;
double dT = (now - lastTime); // / 1000
lastTime = now;
Console.WriteLine(dT);
return dT;
}
}
}
Also, if my analogy of how complex my code is still wasnt enough, heres the update methord from the Particle2DSim class
public void Update(double deltaTime)
{
foreach (Particle2D particle in particles)
{
List<Particle2D> collidedWith = new List<Particle2D>();
Vector2 acceleration = new Vector2();
double influenceSum = 0;
// Calculate acceleration due to Gravity
#region Gravity
foreach (Particle2D particle2 in particles)
{
double dist2 = particle.position.Distance2(particle.position);
double influence = dist2 != 0 ? particle2.mass / dist2 : 0;
acceleration.Add(particle.position.LookAt(particle2.position) * influence);
influenceSum += influence;
if (dist2 < ((particle.radius + particle2.radius) * (particle.radius + particle2.radius)) && dist2 != 0)
{
collidedWith.Add(particle2);
}
}
acceleration.Divide(influenceSum);
#endregion
particle.Update(deltaTime);
// Handle Collisions
#region Collisions
if (collidedWith.Count > 0)
{
Console.WriteLine("Crash!");
double newMass = 0;
double newRadius = 0;
Vector2 newPosition = new Vector2();
Vector2 newVelocity = new Vector2();
newMass += particle.mass;
newRadius += Math.Sqrt(particle.radius);
newPosition += particle.position;
newVelocity += particle.velocity * particle.mass;
particles.Remove(particle);
foreach (Particle2D particle2 in collidedWith)
{
newMass += particle2.mass;
newRadius += Math.Sqrt(particle2.radius);
newPosition += particle2.position;
newVelocity += particle2.velocity * particle2.mass;
particles.Remove(particle2);
}
newPosition.Divide(collidedWith.Count + 1);
newVelocity.Divide(newMass);
AddParticle(newPosition, newVelocity, newMass, newRadius);
}
#endregion
}
}
The problem is that you're using DateTime to try to measure the passage of time. DateTime is meant for representing a date and time, but not for measuring elapsed time.
Use the stopwatch class for measuring time:
Stopwatch sw = new Stopwatch();
sw.Start();
// Do something here
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
// or sw.ElapsedTicks
For more details on the difference, check out Eric Lippert's blog HERE
DeltaTime like in Unity
using System;
class DeltaTimeExample
{
static void Main(string[] args)
{
DateTime time1 = DateTime.Now;
DateTime time2 = DateTime.Now;
// Here we find DeltaTime in while loop
while (true)
{
// This is it, use it where you want, it is time between
// two iterations of while loop
time2 = DateTime.Now;
float deltaTime = (time2.Ticks - time1.Ticks) / 10000000f;
Console.WriteLine(deltaTime); // *float* output {0,2493331}
Console.WriteLine(time2.Ticks - time1.Ticks); // *int* output {2493331}
time1 = time2;
}
}
}
class Program
{
static double DeltaTime;
static double Secondframe;
static double Counter;
static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
while (true)
{
TimeSpan ts = stopWatch.Elapsed;
double FirstFrame = ts.TotalMilliseconds;
DeltaTime = FirstFrame - Secondframe;
Counter += DeltaTime;
Console.WriteLine(Counter);
Secondframe = ts.TotalMilliseconds;
}
}
}
}
DeltaTime class helps you implement the animation.
public class DeltaTime
{
DateTime FirstTime;
public static DeltaTimer CreatePoint()
{
return new DeltaTime(){ FirstTime = DateTime.Now};
}
public TimeSpan GetDeltaTime()
{
if (FirstTime != null)
{
return DateTime.Now - FirstTime;
}
return TimeSpan.FromSeconds(1/60); //If null then return 60 FPS.
}
}
Example 1:
public async void TEST1_Animation(Button button)
{
var pointer= DeltaTime.CreatePoint();
for(double h = 0; h<button.Height;h++)
{
var n= pointer.GetDeltaTime().TotalSeconds;
h = h * n;
await button.Dispatcher.InvokeAsync(() => { button.Height= h; });
await Task.Delay(TimeSpan.FromSeconds(1 / 60 * n));
}
}
And your code will look like this:
static void Update()
{
var Pointer = DeltaTimer.CreatePoint();
while (true)
{
pSim.Update(Pointer.GetDeltaTime().TotalMilliseconds);
}
}

How to delete objects so that they no longer use processor power

Basically, i have an endless number of blocks (each built from class "Enemy") being create, stored into a list, and sent animated across the screen. It does this forever. I want to delete the first block after 100 blocks have been created so as not to use too much processing power. Any ideas?
THIS IS THE CODE FOR THE WHOLE CLASS FOR WHICH THE OBJECTS I WANT TO DELETE:
namespace MovementTestV1
{
class Enemy
{
protected Dispatcher dispatcher;
protected Canvas Background;
protected Label Display;
Int32 waitTime;
double EnemyWidth = 53;
Image EnemyImage;
String FilePathImage;
BitmapImage bitPic;
protected double x, y;
System.Windows.Forms.Timer tmr;
double incrementSize = 5.0;
private int i = 0;
public Enemy(Canvas Background, Dispatcher dispatcher, Dictionary<String, String> keys,Label Display, Int32 waitTime = 100)
{
this.Background = Background;
this.dispatcher = dispatcher;
this.waitTime = 70;
//this.keys = keys;
this.Display = Display;
EnemyImage = new Image();
EnemyImage.Width = EnemyWidth;
FilePathImage = #"RedSqare.png";
bitPic = LoadBitmap(FilePathImage, EnemyWidth);
//tmr = new System.Windows.Forms.Timer();
//tmr.Interval = this.waitTime;
//tmr.Tick += new EventHandler(Position);
//tmr.Start();
}
protected BitmapImage LoadBitmap(String assetsRelativePath, double decodeWidth)
{
BitmapImage theBitmap = new BitmapImage();
theBitmap.BeginInit();
String basePath = System.IO.Path.Combine(Environment.CurrentDirectory, #"assets\");
String path = System.IO.Path.Combine(basePath, assetsRelativePath);
theBitmap.UriSource = new Uri(path, UriKind.Absolute);
theBitmap.DecodePixelWidth = (int)decodeWidth;
theBitmap.EndInit();
return theBitmap;
}
public void Place(double x, double y)
{
EnemyImage.Source = bitPic;
this.x = x;
this.y = y;
Background.Children.Add(EnemyImage);
EnemyImage.SetValue(Canvas.LeftProperty, x);
EnemyImage.SetValue(Canvas.TopProperty, y);
tmr = new System.Windows.Forms.Timer();
tmr.Interval = 10;
tmr.Tick += new EventHandler(Position);
tmr.Start();
}
public void Position(object sender, System.EventArgs e)
{
i++;
if (i < 9000)
{
x -= incrementSize *.3;
}
UpdatePosition();
}
void UpdatePosition()
{
EnemyImage.SetValue(Canvas.LeftProperty, x);
EnemyImage.SetValue(Canvas.TopProperty, y);
}
public double X
{
get
{
return x;
}
set
{
x = value;
}
}
public double Y
{
get
{
return y;
}
set
{
y = value;
}
}
public void Shutdown()
{
tmr.Stop();
}
}
}
public void spawn(object sender, System.EventArgs e)
{
Int32 place = random.Next(1, 4);
Enemy enemy;
i += 2;
if (i % 46 == 0)
{
Int32 Ycoord = random.Next(0, 700);
switch (place)
{
case 1:
enemy = new Enemy(Background, dispatcher, keys, Display, 10);
enemy.Place(1080, Ycoord);
break;
case 2:
enemy = new Enemy(Background, dispatcher, keys, Display, 10);
enemy.Place(1080, Ycoord);
break;
default:
enemy = new Enemy(Background, dispatcher, keys, Display, 10);
enemy.Place(1080, Ycoord);
break;
}
enemies.Add(enemy);
}
if (enemies.Count > 5)
{
//THIS PART DOESNT WORK!!!!!
enemies.RemoveAt(0);
//enemies[1] = 0;
////enemies[2] = null;
//enemies[2].Shutdown();
////enemies[3] = null;
//enemies[3].Shutdown();
////enemies[4] = null;
//enemies[4].Shutdown();
}
}
Hard to say without any code to work off of... However, you could just check to see if your list has over 100 blocks, then setting your first blocks to null. C# has a garbage collector that will clean up the mess.
Edit: or use the Remove method stated above.
for ( int i = 0; i < enemies.size(); ++i )
{
if ( enemies.Count > 5 )
{
enemies.Remove(i);
}
}

Categories

Resources