|
-
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();
}
}
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
|