本文介绍了正在调用计时器,但形状不会移动。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个形状(椭圆)水平移动。为了实现这一点,我使用计时器并增加x值。然而,虽然x值实际上正在改变,但形状根本不能更多。

I am trying to make a shape (ellipse) move horizontally. In order to accomplish that, I use timer and incremented the x value. However, although the x value is actually changing, the shape simply can't more.

这是代码:

namespace flappy_circle
{
    public partial class Form1 : Form
    {

        // Game states and attributes || control the gameLoop
        bool isPlaying = true;
        bool Collided = false;


        // Player Attributes
        int offSetX = 50;
        int offSetY = 50;
        int _Height;
        int _Width;
        float playerXPos;
        float playerYPos;

        // Background Attributes
        int bgOffSet = 50;

        public Form1()
        {
            InitializeComponent();

            DoubleBuffered = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
             ClientSize = new Size(400, 600);
            _Height = ClientSize.Height;
            _Width = ClientSize.Width;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            // Set Player proporties (location and whatnots)
            playerXPos = 100.0f;
            playerYPos = 200.0f;


            SolidBrush myBrush;
            SolidBrush myPlayerBrush;
            SolidBrush myFloorBrush;
            myBrush = new SolidBrush(Color.AliceBlue);
            myPlayerBrush = new SolidBrush(Color.BurlyWood);
            myFloorBrush = new SolidBrush(Color.Brown);
            //Background
            e.Graphics.FillRectangle(myBrush, 0, 0, _Width, _Height);
            //Player
            e.Graphics.FillEllipse(myPlayerBrush, playerXPos, playerYPos, 50, 50);
            //Floor
            e.Graphics.FillRectangle(myFloorBrush, 0, _Height-bgOffSet, _Width, bgOffSet);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            playerXPos = playerXPos + 10.0f;
        }
    }
}


我需要帮助!

I need help, please!

推荐答案

你最初应该在paint事件子设置之外设置playerXPos并且可能是playerYPos并将它们从paint事件子中设置中删除。

You should initially set playerXPos and probably playerYPos outside of the paint event sub and remove them both from being set in the paint event sub.

无论如何,在设置playerXPos之后你需要在timer事件子中使PictureBox无效或刷新,否则PictureBox将不会重绘到playerXPos的新值。

Regardless of that you need to invalidate or refresh the PictureBox in the timer event sub after setting playerXPos or else the PictureBox will not repaint to the new value of playerXPos.


这篇关于正在调用计时器,但形状不会移动。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 11:03