0

I am currently working on a topdown game made in windows forms. In the game, your character rotates after your mouse and you can shoot a shot from your character in the direction of your mouse. To create multiple areas in the game, I chose to use groupboxes as separate areas in the game that you can switch between using buttons but I've run in to a problem. I can no longer rotate or move my character or even shoot within the groupboxes.

I've tried setting breakpoints and discovered that the keyup, keydown and mousemove methods are not being called but I don't know why. For some reason I get an error when i release space in the btnSave_Click method which i marked in the code.

static Image originalImage; bool pRight, pLeft, pUp, pDown;

    string[] Saves;
    Save[] RSaves = new Save[4];
    Save yoursave;
    Save temp;
    int slot;

    NewGame newgame;
    SavedGames savedgames;

    string savedata, name = "";
    int lvl = 1;
    double exp = 0, money = 0;

    int pSpeed = 5;
    double deltaY, deltaX;
    float interval = 7;
    Point start;
    Point nextStart;
    Point cursor;
    float radian;
    const double Rad2Grad = (180 / Math.PI);


    public MainGame()
    {
        InitializeComponent();
        pbPlayer.BringToFront();

        gbxTown.AllowDrop = true;
        gbxQ1.AllowDrop = true;
        pbShot.Location = pbPlayer.Location;
       // newgame = new NewGame();
       // savedgames = new SavedGames();
        originalImage = pbPlayer.Image;
    }

    public void setSaves(string savedata, int slot)
    {
        Saves = savedata.Split('#');
        string a1 = Saves[0];
        string a2 = Saves[1];
        string a3 = Saves[2];
        string a4 = Saves[3];

        RSaves[0] = temp.StringToSaves(temp, a1);
        RSaves[1] = temp.StringToSaves(temp, a2);
        RSaves[2] = temp.StringToSaves(temp, a3);
        RSaves[3] = temp.StringToSaves(temp, a4);

        yoursave = RSaves[slot - 1];
        name = yoursave.getName();
        this.slot = slot;

        Controls.Add(pbPlayer);
        Controls.Add(pbShot);
    }

    private void MainGame_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.A)
        {
            pRight = false;
            pLeft = true;
        }

        else if (e.KeyData == Keys.D)
        {
            pRight = true;
            pLeft = false;
        }

        else if (e.KeyData == Keys.S)
        {
            pUp = true;
            pDown = false;
        }

        else if (e.KeyData == Keys.W)
        {
            pUp = false;
            pDown = true;
        }

        if (e.KeyData == Keys.Space)
        {
            start = pbPlayer.Location;
            cursor = Cursor.Position;
            nextStart = start;

            deltaY = cursor.Y - start.Y;
            deltaX = cursor.X - start.X;

            double test = Angle(start, cursor);

            radian = (float)(Angle(start, cursor) - 175);

            timer2.Enabled = true;
        }

    }

    private void MainGame_KeyUp_1(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.A)
        {
            pLeft = false;
        }

        else if (e.KeyData == Keys.D)
        {
            pRight = false;
        }

        else if (e.KeyData == Keys.S)
        {
            pUp = false;
        }

        else if (e.KeyData == Keys.W)
        {
            pDown = false;
        }

        if (e.KeyData == Keys.Space)
        {
            timer2.Stop();
            pbShot.Location = start;
        }

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        MovePlayer();
        //checkCollision();
    }

    private void btnNextArea_Click(object sender, EventArgs e)
    {
        pbPlayer.Parent = gbxQ1;
        pbShot.Parent = gbxQ1;
        gbxQ1.Location = new Point(0, 0);
        gbxTown.Location = new Point(605, 0);
        pbPlayer.Location = new Point(100, 200);
        pbShot.Location = pbPlayer.Location;
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        newgame = new NewGame();
        savedgames = new SavedGames();

        RSaves[slot - 1] = new Save(name, money, lvl, exp);

        for (int i = 0; i < 4; i++) //When the Spacebar is released, a System.IndexOutOfRangeException error happens for i.
        {
            Saves[i] = RSaves[i].SavesToString();
        }

        savedata = Saves[0] + Saves[1] + Saves[2] + Saves[3];


        System.IO.File.WriteAllLines(@"saves.txt", Saves);
        newgame.setSaves(savedata);
        savedgames.setSaves(savedata);
    }

    private void btnBack_Click(object sender, EventArgs e)
    {
        pbPlayer.Parent = gbxTown;
        pbShot.Parent = gbxTown;
        gbxTown.Location = new Point(0, 0);
        gbxQ1.Location = new Point(605, 0);
        pbPlayer.Location = new Point(100, 200);
        pbShot.Location = pbPlayer.Location;
    }

    private void MainGame_MouseMove_1(object sender, MouseEventArgs e)
    {
        var y2 = e.Y;
        var y1 = (this.pbPlayer.Location.Y + (this.pbPlayer.Height / 2));
        var x2 = e.X;
        var x1 = (this.pbPlayer.Location.X + (this.pbPlayer.Width / 2));

        var angle = (float)Math.Atan2((y1 - y2), (x1 - x2));

        pbPlayer.Image = RotateImage(originalImage, (angle * 57));
    }

    private double Angle(Point start, Point end)
    {
        return (float)Math.Atan2(end.Y - start.Y, end.X - start.X) * 57;
    }


    private void timer2_Tick_1(object sender, EventArgs e)
    {
        nextStart.X -= Convert.ToInt16(interval * ((float)Math.Cos(radian / Rad2Grad)));
        nextStart.Y -= Convert.ToInt16(interval * ((float)Math.Sin(radian / Rad2Grad)));

        pbShot.Location = nextStart;
    }


    public static Image RotateImage(Image img, float rotationAngle)
    {
        Bitmap bmp = new Bitmap(img.Width, img.Height);
        Graphics gfx = Graphics.FromImage(bmp);

        gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
        gfx.RotateTransform(rotationAngle);
        gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gfx.DrawImage(img, new Point(0, 0));
        gfx.Dispose();

        return bmp;
    }

    private void MovePlayer()
    {
        if (pRight == true && pbPlayer.Left < 900)
        {
            pbPlayer.Left += pSpeed;
        }

        else if (pLeft == true && pbPlayer.Left > 0)
        {
            pbPlayer.Left -= pSpeed;
        }

        else if (pUp == true && pbPlayer.Top < 600)
        {
            pbPlayer.Top += pSpeed;
        }

        else if (pDown == true && pbPlayer.Top > 0)
        {
            pbPlayer.Top -= pSpeed;
        }
    }

The expected outcome is for the movement, aiming and firing to work as intended within the groupboxes but currently they do not execute in the code while in the groupboxes. It works fine in the form but not in the groupboxes. I would appriciate any help.

jallebuS
  • 1
  • 1
  • a) this is way too much code for a proper question here. b) controls can't be rotated. (and they don't support tansparency). Best give up on using controls and draw all graphics onto one pbox canvas. – TaW Apr 23 '19 at 14:02
  • I think the issue is not the GroupBox but the buttons. They keep the focus so the keyboard events are fired on the button, not the window. Try to set the focus on your window by calling this.Focus() at the end of your btn_Click functions – KiwiJaune Apr 23 '19 at 14:04
  • [See All Key Events with KeyPreview](http://www.csharp411.com/see-all-key-events-with-keypreview/). – Olivier Jacot-Descombes Apr 23 '19 at 14:14
  • I tried what you said KiwiJaune but it didn't work. The methods that I have in the code work in the form itself but when they're inside the groupboxes it simply doesn't work anymore. For example, the mousemoverelated methods work when im moving the mouse in the form outside of the groupbox but not when the cursor is within it. Im pretty sure the problem has to do with the method "not recognizing" movement in the groupboxes but I have no clue how to go about solving it. – jallebuS Apr 23 '19 at 14:18

0 Answers0