图片盒无法正常移动 [英] Picturebox not moving properly

查看:73
本文介绍了图片盒无法正常移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我制作了一个非常简单的小应用程序来测试使对象在按下按钮时看起来像是跳了起来,并且工作正常,但是我正在跳的图片框似乎既停留在起始位置,又处于跳跃状态.我试图寻找答案,但不确定如何描述问题.问题是这样的:

问题的外观

代码如下:

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!

推荐答案

糟糕,我刚刚解决了自己的问题...
Oops just solved my own problem ... had
InitializeComponent();


两次,取出一个使它起作用....

抱歉浪费时间...


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

Sorry for wasting time...


这篇关于图片盒无法正常移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆