how to make an object move up, down, left and right using a loop in visual studio using c#
i can make a picture in a picture box move right and down using a for loop, this is what my开发者_开发问答 code looks like.
private void xAnimeTimer_Tick(object sender, EventArgs e)
{
int count;
this.xAnimeTimer.Stop();
// start point end point speed
for (count = 0; count <= 1350; count+=2)
{
this.xAnimePictureBox.Left = count;
}
for (count = 0; count <= 810; count += 2)
{
this.xAnimePictureBox.Top = count;
}
I cant figure out how to get the picture to move up and left, i can only get it to move right and down.
Is your this.xAnimePictureBox placed at the center already? If so, negating your counts would give you the mirrored directions(right down for left top).
If it is not, then move ur this.xAnimePictureBox to center, and negate your .Top and .Left.
By means of the center, i assume your 1350 is the width, and 810 is the height? the center will then be 1350/2 , 810/2 and for pairing the center of your picturebox to the center of the canvas (or whatever that's holding the pictureBox) will be Left = 1350/2 - (pictureBox.Width/2) , Top = 810/2 -(pictureBox.Height/2).
Invert what you have:
this.xAnimeTimer.Stop();
// start point end point speed
for (count = 1350; count >= 0; count-=2)
{
this.xAnimePictureBox.Left = count;
}
for (count = 810; count >= 0; count -= 2)
{
this.xAnimePictureBox.Top = count;
}
The origin (0, 0) for screen graphics is the top left hand corner of your drawing area. So smaller Top values will move the item higher up the screen until you get to 0, then th eitem will move off the top of the screen. Smaller Left values on the other hand will move the item left.
精彩评论