Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How do I get my character to keep moving?

$
0
0
I want my picturebox to move continuously when I press one of the arrow keys. (tap right arrow key and it stars moving right)? I am using this to make it move.
 void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            int x = picCharlie.Location.X;
            int y = picCharlie.Location.Y;
            if (e.KeyCode == Keys.Right)
            {
                
                
                Boolean didIHitAWall = false;
                foreach (Control c in this.Controls)
                {
                    if (c.Name.Contains("lbl"))
                    {
                        Rectangle newCharlie = new Rectangle(picCharlie.Location.X + 4, picCharlie.Location.Y, picCharlie.Width, picCharlie.Height);
                        if (newCharlie.IntersectsWith(c.Bounds))
                        {
                            didIHitAWall = true;
                        }
                    }
                }
                            if (!(didIHitAWall)) x += 4;
            }
            else if (e.KeyCode == Keys.Left)
            {
                Boolean didIHitAWall = false;
                foreach (Control c in this.Controls)
                {
                    if (c.Name.Contains("lbl"))
                    {
                        Rectangle newCharlie = new Rectangle(picCharlie.Location.X - 4, picCharlie.Location.Y, picCharlie.Width, picCharlie.Height);
                        if (newCharlie.IntersectsWith(c.Bounds))
                        {
                            didIHitAWall = true;
                        }
                    }
                }
                            if (!(didIHitAWall)) x -= 4;
            }
            else if (e.KeyCode == Keys.Up)
            {
                Boolean didIHitAWall = false;
                foreach (Control c in this.Controls)
                {
                    if (c.Name.Contains("lbl"))
                    {
                        Rectangle newCharlie = new Rectangle(picCharlie.Location.X, picCharlie.Location.Y - 4, picCharlie.Width, picCharlie.Height);
                        if (newCharlie.IntersectsWith(c.Bounds))
                        {
                            didIHitAWall = true;
                        }
                    }
                }
                            if (!(didIHitAWall)) y -= 4;
            }
            else if (e.KeyCode == Keys.Down)
            {
                Boolean didIHitAWall = false;
                foreach (Control c in this.Controls)
                {
                    if (c.Name.Contains("lbl"))
                    {
                        Rectangle newCharlie = new Rectangle(picCharlie.Location.X, picCharlie.Location.Y + 4, picCharlie.Width, picCharlie.Height);
                        if (newCharlie.IntersectsWith(c.Bounds))
                        {
                            didIHitAWall = true;
                        }
                    }
                }
                            if (!(didIHitAWall)) y += 4;
            }


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>