O'Reilly Forums: The Hive - Bees Don't Move - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

The Hive - Bees Don't Move Chapter 13

#1 User is offline   jimbojambo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 04-April 11

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 File(s)


This post has been edited by jimbojambo: 20 May 2014 - 12:22 PM

0

#2 User is offline   jimbojambo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 04-April 11

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
0

#3 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 806
  • Joined: 08-October 08
  • 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
0

#4 User is offline   jimbojambo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 04-April 11

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users