Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone,

I made a very simple little application to test making an object appear to jump on a button press, and it works fine however the picture box I'm making jump appears to be staying in the starting position as well as jumping. I tried to search for an answer but wasn't quite sure how to describe the problem. The problem is like this:

Visual of problem

Here's the code:

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 Jumping
{
    public partial class Form1 : Form
    {
        bool jumping = false;
        bool falling = false;
        int startingElevation = 0;

        public Form1()
        {
            InitializeComponent();
            Timer Clock;
            Clock = new Timer();
            Clock.Interval = 1;
            Clock.Start();
            Clock.Tick += new EventHandler(Timer_Tick);
            InitializeComponent();
        }

        public void Timer_Tick(object sender, EventArgs eArgs)
        {
            int currentElevation = PBObject.Top + PBObject.Height;

            if (jumping == true)
            {
                if ((currentElevation > (startingElevation - 20)) && (currentElevation <= (startingElevation)))
                {
                    PBObject.Top -= 5;
                }
                else if ((currentElevation > (startingElevation - 40)) && (currentElevation <= (startingElevation - 20)))
                {
                    PBObject.Top -= 4;
                }
                else if ((currentElevation > (startingElevation - 60)) && (currentElevation <= (startingElevation - 40)))
                {
                    PBObject.Top -= 3;
                }
                else if ((currentElevation > (startingElevation - 80)) && (currentElevation <= (startingElevation - 60)))
                {
                    PBObject.Top -= 2;
                }
                else if ((currentElevation > (startingElevation - 100)) && (currentElevation <= (startingElevation - 80)))
                {
                    PBObject.Top -= 1;
                }
                else if (currentElevation <= (startingElevation - 100))
                {
                    jumping = false;
                    falling = true;
                }
            }
            else if (falling == true)
            {
                if ((currentElevation >= (startingElevation - 100)) && (currentElevation < (startingElevation - 80)))
                {
                    PBObject.Top += 1;
                }
                else if ((currentElevation >= (startingElevation - 80)) && (currentElevation < (startingElevation - 60)))
                {
                    PBObject.Top += 2;
                }
                else if ((currentElevation >= (startingElevation - 60)) && (currentElevation < (startingElevation - 40)))
                {
                    PBObject.Top += 3;
                }
                else if ((currentElevation >= (startingElevation - 40)) && (currentElevation < (startingElevation - 20)))
                {
                    PBObject.Top += 4;
                }
                else if ((currentElevation >= (startingElevation - 20)) && (currentElevation < (startingElevation)))
                {
                    PBObject.Top += 5;
                }
                else if (currentElevation >= startingElevation)
                {
                    falling = false;
                    PBObject.Top = startingElevation - PBObject.Height;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((jumping == false) && (falling == false))
            {
                startingElevation = PBObject.Top + PBObject.Height;

                jumping = true;
            }
        }
    }
}


Is the problem something to do with needing to refresh the form or something?

If anyone has an answer I'll be very grateful!
Posted

1 solution

Oops just solved my own problem ... had
C#
InitializeComponent();

twice and taking one out makes it work ....

Sorry for wasting time...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900