|
-
May 22nd, 2011, 12:05 AM
#1
Double Var keeps adding to increment
-Synopsis-
I've recently developed a simple physics program, which presents a modifiable velocity setting.
It updates the current time(in secs) as well as the current position which happens to add to the
current position the missile image is set to.
-Problem-
Overall the program works just fine, there just happens to be one problem , in order to update the
newtime I've created a double variable called "Currentime" which is set equal to the current text in
the textbox plus the time increment(0.5). This is called by a timer object which is created for the
form and calls it every 0.05 seconds. The thing is though that everytime i reset the time fields to
zero it for some reason adds like this 0.5 ,1.0, 1.5, 2.0 etc (1st round) then 1.0 ,2.0, 3.0., 4.0,etc(2nd round) and keeps increasing
if you need more code or want a copy i can provide it i really need help i don't know how to solve it
to see exactly what i mean here is segment of the code
private void button1_Click(object sender, EventArgs e)// If this button is clicked start the timer
{
timestarted = true;
this.timer1.Enabled = true;
this.timer1.Interval = 100;
this.timer1.Start();
this.timer1.Tick += new EventHandler(timer1_Tick);
}
void timer1_Tick(object sender, EventArgs e)
{
this.textBox1.ResumeLayout();
Update();
}
public void Update()
{
double newtime = double.Parse(this.textBox1.Text) + double.Parse(this.textBox4.Text);
this.pictureBox1.Refresh();
//textbox 1 = time text textbox 4 = time increment( 0.5)
System.Drawing.Image Missile = Image.FromFile(@"C:\Users\Brandon Fleury\Documents\Visual Studio 2008\2dProjectileTest\2dProjectileTest\Resources\Missile.jpg");
InitialMissileHeight = 332.0;
InitialTime = 0.0;
InitialVelocity = 20.0;
InitialAcceleration = 0.0;
// Y-Position -- Position(Final) = Posiiton(Initial) + Velocity(Initial)* Current Time + 1/2*Acceleration* Current Time*Current Time
double Yposition = InitialMissileHeight - double.Parse(this.textBox1.Text) * Double.Parse(this.textBox1.Text); ;
double newheight = InitialMissileHeight - Yposition;
if (newheight <500.0)
{
this.textBox1.Text = newtime.ToString();
this.textBox3.Text = newheight.ToString();
this.textBox3.Update();
this.pictureBox1.CreateGraphics().DrawImage(Missile, 225, Convert.ToInt32(Yposition), 10, 10);
this.pictureBox1.Update();
}
}
-
May 23rd, 2011, 01:34 AM
#2
Re: Double Var keeps adding to increment
Just for sure, what is expected behavior?
1st: 0.0 0.5 1.0 1.5 2.0 ...
2nd: 0.0 0.5 1.0 1.5 2.0 ...
3rd: 0.0 0.5 1.0 1.5 2.0 ...
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
May 23rd, 2011, 12:24 PM
#3
Re: Double Var keeps adding to increment
Why do you add another event handler with every button click?
-
May 23rd, 2011, 01:02 PM
#4
Re: Double Var keeps adding to increment
 Originally Posted by zips
Why do you add another event handler with every button click?
Code:
this.timer1.Tick += new EventHandler(timer1_Tick);
I would check this first. Either remove the event handler later in the code like so...
Code:
this.timer1.Tick -= new EventHandler(timer1_Tick);
Or add the event handler outside of the button click but before the button click takes place, like a constructor or another method thats called on the constructor. This way its only added once.
-
May 25th, 2011, 03:09 PM
#5
Re: Double Var keeps adding to increment
ah presto that was it thanks guys i appreciate it it works just fine now, i have to take note of that for future projects thanks tons and it wasn't intentional btw just a small thing i overlooked
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|