none
c# moving an image RRS feed

  • Question

  • Hi, I have recently found code to move an image up, right, left and down. this works perfectly. the location is being shown by 'pictureBox1.Location = new Point (x,y);'. This is all fine. But now I want to add it so that when I click the spacebar the image jumps. So on screen you see it going up then down to original position. I did the following code:

    "else if (e.KeyCode == Keys .Space) jumpy -=35 ; jumpy += 50"

    (the text I input now in this forum is blue for some reason)

    but anyway.. I used += 50' to see if it worked and yes it does, but it goes up and down in one go instead of showing it happening.

    'jumpy' is set before at :

    int jumpy = pictureBox1.Location.Y;

    I can not get all keys to work at once as the 'new Point(x,y)' will not show my space bar working unless I replaye jumpy with the (x or y) in the 'new point (x,y).

    Here is all my code just to make it easier to understand: 

    namespace MovingImg
    {
        public partial class Form1 : Form
        {
     public Form1()
            {
                InitializeComponent();
                KeyDown += new KeyEventHandler(Form1_KeyDown);
            }
    
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                int x = pictureBox1.Location.X;
                int y = pictureBox1.Location.Y;
                //int jumpx = pictureBox1.Location.X;
                int jumpy = pictureBox1.Location.Y;
    
                if (e.KeyCode == Keys.Right) x += 5;
                else if (e.KeyCode == Keys.Left) x -= 5;
                else if (e.KeyCode == Keys.Up) y -= 5;
                else if (e.KeyCode == Keys.Down) y += 5;
                else if (e.KeyCode == Keys.Space) jumpy -= 35; jumpy += 50;
    
                pictureBox1.Location = new Point(x,y);
                
            }

     
    Sunday, January 6, 2013 6:53 PM

Answers

  • Your program tries to process your instructions as fast as possible, not gradually. For a gradual motion, you must use a timer, or something to hold the image for a moment; your image must go through every position in the way. This (untested) code might work:

    using System.Threading

    /* EDIT: don't forget to add this and the respective reference

    if not already present */

    private void Form1_KeyDown(object sender, KeyEventArgs e) { int x = pictureBox1.Location.X; int y = pictureBox1.Location.Y; // int jumpx = pictureBox1.Location.X; // int jumpy = pictureBox1.Location.Y; /* you don't need 'jumpx' and 'jumpy', you can use 'x' and 'y'; actually, in the manner you were using them, I don't know how it even worked, as ints aren't references*/ if (e.KeyCode == Keys.Right) x += 5; else if (e.KeyCode == Keys.Left) x -= 5; else if (e.KeyCode == Keys.Up) y -= 5; else if (e.KeyCode == Keys.Down) y += 5; else if (e.KeyCode == Keys.Space) { for (int i = 0; i <= 35; i++) { Thread.Sleep(50); // stop for 0,05 seconds y -= 1; pictureBox1.Location = new Point(x, y); } for (int i = 0; i <= 50; i++) { Thread.Sleep(50); // stop for 0,05 seconds y += 1; pictureBox1.Location = new Point(x, y); } } pictureBox1.Location = new Point(x,y); }

    Here I used 'Thread.Sleep', that stops the thread for the specified amount of milisseconds, you can change them for faster or slower movement (1/20 of a second was just a blind guess, you'll probably want a different value). The 'for' loops iterate the amount of times you sepecified, changing the position each time, after the waiting.

    Also, this code likely can be optimized, what I wrote might be rather expensive for a relativly simple thing. I hope this works properly and helps you.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel



    • Edited by JMCF125 Monday, January 7, 2013 10:07 PM correcting a coding error whilst in a previous edit
    • Proposed as answer by JMCF125 Thursday, January 10, 2013 10:03 PM
    • Marked as answer by Lisa ZhuModerator Friday, January 11, 2013 7:17 AM
    Sunday, January 6, 2013 9:23 PM

All replies

  • Your program tries to process your instructions as fast as possible, not gradually. For a gradual motion, you must use a timer, or something to hold the image for a moment; your image must go through every position in the way. This (untested) code might work:

    using System.Threading

    /* EDIT: don't forget to add this and the respective reference

    if not already present */

    private void Form1_KeyDown(object sender, KeyEventArgs e) { int x = pictureBox1.Location.X; int y = pictureBox1.Location.Y; // int jumpx = pictureBox1.Location.X; // int jumpy = pictureBox1.Location.Y; /* you don't need 'jumpx' and 'jumpy', you can use 'x' and 'y'; actually, in the manner you were using them, I don't know how it even worked, as ints aren't references*/ if (e.KeyCode == Keys.Right) x += 5; else if (e.KeyCode == Keys.Left) x -= 5; else if (e.KeyCode == Keys.Up) y -= 5; else if (e.KeyCode == Keys.Down) y += 5; else if (e.KeyCode == Keys.Space) { for (int i = 0; i <= 35; i++) { Thread.Sleep(50); // stop for 0,05 seconds y -= 1; pictureBox1.Location = new Point(x, y); } for (int i = 0; i <= 50; i++) { Thread.Sleep(50); // stop for 0,05 seconds y += 1; pictureBox1.Location = new Point(x, y); } } pictureBox1.Location = new Point(x,y); }

    Here I used 'Thread.Sleep', that stops the thread for the specified amount of milisseconds, you can change them for faster or slower movement (1/20 of a second was just a blind guess, you'll probably want a different value). The 'for' loops iterate the amount of times you sepecified, changing the position each time, after the waiting.

    Also, this code likely can be optimized, what I wrote might be rather expensive for a relativly simple thing. I hope this works properly and helps you.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel



    • Edited by JMCF125 Monday, January 7, 2013 10:07 PM correcting a coding error whilst in a previous edit
    • Proposed as answer by JMCF125 Thursday, January 10, 2013 10:03 PM
    • Marked as answer by Lisa ZhuModerator Friday, January 11, 2013 7:17 AM
    Sunday, January 6, 2013 9:23 PM
  • I tried the code but it didn't work. It hihglights the 'Thread'.
    Monday, January 7, 2013 12:59 PM
  • I'm sorry, I forgot to add the using directive. You need to add "using System.Threding" and perhaps add a reference to it in your project. Then it should work.

    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    • Marked as answer by Lisa ZhuModerator Friday, January 11, 2013 7:17 AM
    • Unmarked as answer by cmurphy96 Friday, January 11, 2013 1:06 PM
    Monday, January 7, 2013 6:15 PM
  • Yes, this is perfect thank you, it goes to low down on the way back from the jump. I will try and correct this.
    Friday, January 11, 2013 1:07 PM