1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Game2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void Move_Ship(int X_Coord ,int Y_Coord, string Positioning)
        {
            if(Positioning == "Rechts")
            {
                X_Coord += 5;
                this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
            };

            if (Positioning == "Links")
            {
                X_Coord -= 5;
                this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
            };

            if (Positioning == "Up")
            {
                Y_Coord -= 5;
                this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
            };

            if (Positioning == "Down")
            {
                Y_Coord += 5;
                this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
            };
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Control.ModifierKeys == Keys.Up)
            {
                Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Up");
            };

            if (Control.ModifierKeys == Keys.Down)
            {
                Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Down");
            };

            if (Control.ModifierKeys == Keys.Left)
            {
                Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Links");
            };

            if (Control.ModifierKeys == Keys.Right)
            {
                Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Rechts");
            };           
        }
    }
}

If I did this right my piture should be moving the but it doesn't do anything at all. Did I do something wrong and if so please tell me?

Theraot
  • 31,890
  • 5
  • 57
  • 86
fox125
  • 97
  • 2
  • 11
  • At first glance I can tell you that you don't need those ";" after the `if` blocks. – Theraot Dec 25 '13 at 00:07
  • @Theraot As you edited out I'm at the start of my C# knowledge here. The ; really confuse me but I'll get used to it. Anyways still doesn't move. – fox125 Dec 25 '13 at 00:10
  • If that were a posible soulution I would have posted it as an answer ^_^, it is just a clarification. By the way I wasn't who removed that part, that was Alexei Levenkov. I fixed the indentation. – Theraot Dec 25 '13 at 00:13
  • @fox125 "Thank you" notes and all kinds of "new here", "searched a lot" don't add much information to posts, so generally removed (feel free to discuss on META starting with [this](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) ). Instead of that some information on your investigation would be much more useful (i.e. "while debugging `Control.ModifierKeys` newer shows `Key.Right` value" or something like this). – Alexei Levenkov Dec 25 '13 at 00:18

1 Answers1

2

One possible problem is usage of Control.ModifierKeys

Gets a value indicating which of the modifier keys (SHIFT, CTRL, and ALT) is in a pressed state.

If you want to use direction keys - listen for keyDown event and save direction there. Sample and details Control.KeyDown:

  private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
     // Determine whether the keystroke is a number from the top of the keyboard. 
     if (e.KeyCode == Keys.Down)
     { 
        direction = "Down";
     }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • considering I learned that code over here http://stackoverflow.com/questions/1100285/how-to-detect-the-currently-pressed-key that is probably true – fox125 Dec 25 '13 at 00:12
  • @fox125 as Alexei mentions, you should use a keyboard event for this. Be it KeyDown, KeyUp or KeyPress. Also you don't need the Timer. If you are having problems with binding the event, I want to encourage you to [learn more about Events](http://stackoverflow.com/a/20734571/402022). – Theraot Dec 25 '13 at 00:17
  • @Theraot - actually timer may be good choice as it provides constant speed of events and hence consistent movement speed, unlike keyboard events (obviously it depends on the goal - constantly moving object like a ball vs. moving only while key is pressed). – Alexei Levenkov Dec 25 '13 at 00:20
  • Kind of. The timer will be affected by other events, yet it is true that it will give a more constant behaviour. The goal seems to be a game (given the namespace name) for that I would be measuring time and having a declared velocity in pixels per second, also I would use SDL or OpenGL and a main loop, no event handlers. But that is a whole level beyond what @fox125 can accomplish at this stage. – Theraot Dec 25 '13 at 00:24