Move PictureBox location with threading

I am creating a catching game where eggs are dropped. In my Panel subclass, I have this code

public void startGame()
    {
        Thread t = new Thread(new ThreadStart(game));
        t.Start();
    }

private void game()
    {
        bool run = true;

        int level = 1;

        while (run)
        {
            Egg egg = dropper.selectEgg();
            int speed = dropper.getSpeed(level);

            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate { 
                    this.Controls.Add(egg);
                    egg.setInitialLocation(dropper.selectPosition());

                    int x = egg.Location.X;
                    int y = egg.Location.Y;

                    while (y <= 1000)
                    {
                        egg.setCurrentLocation(x, dropper.drop(egg, speed));
                        y = egg.Location.Y;
                    }
                }));
            }
            else
            {
                this.Controls.Add(egg);
                egg.setInitialLocation(dropper.selectPosition());

                int x = egg.Location.X;
                int y = egg.Location.Y;

                while (y <= 1000)
                {
                    egg.setCurrentLocation(x, dropper.drop(egg, speed));
                    y = egg.Location.Y;
                }
            }
            Thread.Sleep(3000);
        }
    }

Egg is a subclass of PictureBox and I want to change its location on loop so it looks like the eggs are dropping. I use the EggDropper subclass with this method:

public int drop(Egg egg, int speed)
    {

        int y = egg.Location.Y;
        y += speed;

        return y;
    }

but somehow, I don't see any of the Egg objects dropping. I'm guessing it is a problem with the thread accessing the PictureBox subclass? But I can't seem to find any solution online.

Thank you very much in advance.

Answers


You call drop on the main UI thread. This quickly runs a loop that increments y until it is > 1000. The UI cannot update while this loop is running, so all you will see is the egg at the bottom of the screen when drop finishes and the UI can run its message loop again.

The solution is to change drop to just decrement y once and then return control to your game loop. You will have to move the y <= 1000 check to this loop as well.


UPDATE:

Your "step" is an iteration of the while( run ) loop - that's where you have the Sleep to control the animation. You must only make one update to egg.Location.Y on each "step" - don't run the whole while( y <= 1000 ) loop on each "step".


Need Your Help

Can NovellAuthenticators (LDAP) be add to Weblogic using WLST Offline?

java ldap weblogic wlst

I would like to create a WLST script to create my Weblogic domain. However I'm having problems adding the LDAP config.

How to disable NSLog all over the app?

ios objective-c xcode c-preprocessor nslog

I want to disable NSLog() across all instances in an app. I found some code that does that:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.