I've written a code to move images from specific locations with timer
and pictureBox
.
But my code doesn't work properly because images don't move on the path I want to.
what I want is:
- create images from points (10,Y0).
- move horizontally right to reach point (500,Y0).
- move vertically down or up to reach point (500,750) and stop here for example 7 second.
- then, move again horizontally right and go to point (700,750) and stop here for example 8 second.
- then, move again horizontally right and go to point (750,750).
- at this point, if (complete == true) the `pictureBox` must hide and back to (10,Y0)
- if (complete == false) then
if (up == true)
- move vertically up to reach point (750,900) and and
- move horizontally right and go to (900,900) and stop here for example 10 second.
- then, then, move again horizontally left and go to point (500,900).
- then, move vertically down to reach point (500,750).
else if (down == true)
- move vertically down to reach point (750,600) and and
- move horizontally right and go to (900,600) and stop here for example 10 second.
- then, then, move again horizontally left and go to point (500,600).
- then, move vertically up to reach point (500,750).
I've written some codes but as I said, It's doesn't move correctly...
one more thing: how I can implement the waits??! (when reach for exapmle (900,600) we must wait 10 second or wait until something outside allow the image to move).
Please help me...
Here is my codes so far:
private int k = 0;
void timer_Tick(object sender, EventArgs e)
{
k++;
int x = p.Location.X;
int y = p.Location.Y;
if (k <= 250)
开发者_JAVA技巧 p.Location = new Point(x + 2, y);
else if (k <= 400)
{
// if we are moving up of point (500,750)
if (y < 750)
p.Location = new Point(x, y + 1);
// if we are moving down of point (500,750)
if (y > 750)
p.Location = new Point(x, y - 1);
}
// *** wait HERE.
else if (k <= 500)
p.Location = new Point(x + 2, y);
// *** wait HERE.
else if (k <= 550)
p.Location = new Point(x + 2, y);
else if (k <= 700)
{
if (complete == true)
p.Location = new Point(x, y + 1);
else
p.Location = new Point(x, y - 1);
}
else if (k <= 800)
p.Location = new Point(x + 2, y);
// ***** wait HERE
else if (k <= 1200)
p.Location = new Point(x - 2, y);
else if (k <= 1400)
{
if (complete == true)
p.Location = new Point(x, y - 1);
else
p.Location = new Point(x, y + 1);
}
else
timer1.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
timer1.Interval = 15;
timer1.Tick += new EventHandler(timer_Tick);
}
To be honest, because I'm not entirely sure what it's actually doing I'm not going to offer a picturebox solution. Animation, timing etc is, in my experience, never easy to implement using timers, primarily because the kind of timers you're using don't guarantee that they fire exactly when they should - since they require the windows message pump to be empty for the timer message to get through. It's not that it's impossible or anything, indeed my first work with animation was also done with timers.
If the code you're writing is to satisfy some exercise in computer animation (i.e. homework?) then you will have to learn this as part of trying to solve the problem or somebody more generous than I will no doubt give you the answer.
For my part, do this instead in WPF and use its animation and storyboard functionality.
This way you can specify your timelines and paths (linear paths are simple with WPF) and then you can use events for the decisions. Managing the actual animation then is no longer your problem - you just specify where things need to be and how long they should take.
The final result is likely to be far superior on screen as well, since WPF uses a 'proper' rendering loop akin to the kind of thing used in games.
Sorry if this answer isn't helpful.
精彩评论