Jump to content


Photo

The Hive - Bees Don't Move


  • Please log in to reply
3 replies to this topic

#1 jimbojambo

jimbojambo

    New Member

  • Members
  • Pip
  • 6 posts

Posted 20 May 2014 - 12:19 PM

I'm on p14 chapter 13 The Hive.

The application will run but the bees don't move/fly anywhere, they just stay in the hive flapping their wings.

Any ideas what's going on? :huh:

These never get called

private void MoveBeeFromHiveToField(BeeControl beeControl)
{
hiveForm.Controls.Remove(beeControl);
beeControl.Size = new Size(20, 20);
fieldForm.Controls.Add(beeControl);
beeControl.BringToFront();
}


private void MoveBeeFromFieldToHive(BeeControl beeControl)
{
fieldForm.Controls.Remove(beeControl);
beeControl.Size = new Size(40, 40);
hiveForm.Controls.Add(beeControl);
beeControl.BringToFront();
}

Attached Files


Edited by jimbojambo, 20 May 2014 - 12:22 PM.


#2 jimbojambo

jimbojambo

    New Member

  • Members
  • Pip
  • 6 posts

Posted 22 May 2014 - 10:22 AM

I did a dump of the bees location and I can see they move as far as their location property is concerned

Bee 6 113 108
Bee 5 133 165
Bee 4 93 132
Bee 3 96 148
Bee 2 58 121
Bee 1 78 145
Bee 4 44 205
Bee 4 44 205
Bee 6 365 202
Bee 1 347 202
Bee 6 365 202
Bee 1 347 202
Bee 2 398 253
Bee 5 497 238
Bee 4 185 77
Bee 2 398 253
Bee 4 185 77
Bee 5 497 238
Bee 3 497 238
Bee 3 497 238
Bee 6 185 77
Bee 1 185 77
Bee 4 185 77
Bee 6 185 77
Bee 1 185 77
Bee 6 185 77
Bee 2 185 77
Bee 1 185 77
Bee 2 185 77
Bee 2 185 77
Bee 5 185 77
Bee 3 185 77
Bee 3 185 77
Bee 3 185 77
Bee 5 185 77
Bee 4 104 235
Bee 4 104 235
Bee 6 347 202
Bee 6 347 202
Bee 3 104 235
Bee 2 347 202
Bee 3 104 235
Bee 2 347 202
Bee 4 185 77
Bee 5 185 77
Bee 4 185 77
Bee 4 185 77
Bee 1 509 259
Bee 1 509 259
Bee 3 185 77
Bee 3 185 77
Bee 3 185 77
Bee 6 185 77
Bee 6 185 77
Bee 5 245 220

#3 AndrewStellman

AndrewStellman

    Andrew Stellman

  • O'Reilly Author
  • PipPipPipPipPipPipPipPipPipPipPip
  • 817 posts
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 22 May 2014 - 10:07 PM

Wow, that's a tricky bug. It turns out that you have two instances of World. One instance is the active simulation, the other is being used for rendering -- and when it tries to look up its bees in beeLookup, they haven't moved (because the simulation has been updating the first World instance).

You can see this for yourself by doing this:


1. Run the program.
2. Put a breakpoint in Renderer.Render().
3. When the program breaks, add a watch for "world".
4. Right-click on your world watch and choose "Make Object ID" -- we teach about this feature in the third edition. It assigns an ID to each unique object. In this case, it will assign this instance of World ID #1.
5. Put a breakpoint in Bee.Go() and keep hitting F5 until it breaks there.
6. Right-click on your world watch and choose "Make Object ID" again. This time it should give it ID #2. This means that the Bee object's world field points to a different object than the Renderer's world field.

When I search for "new World(" in the solution, I see that you're creating a new instance of World in both resetSimulation_click() and ResetSimulator() in Form1.cs. You only want to create a single instance of World.

(If you want to learn a little bit about design patterns, this is a great opportunity to learn about the Singleton pattern.)

Anyway, you are 100% forgiven for not being able to find that one. It was a really nasty little bug! :)
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com

#4 jimbojambo

jimbojambo

    New Member

  • Members
  • Pip
  • 6 posts

Posted 23 May 2014 - 08:43 AM

Thank you very much your response Andrew :) My simulator now works, I would never have guessed that was happening. I think i'll be making more use of the watch window.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users