This was a tough one for me.

I knew enough about Java that it made sense to create a Firefly class. Something that would let me describe a firefly, maybe declare its position, size, etc — but also so I have a way to create multiple instances of my firefly.

With that, here’s my class below. But first, a few notes.

  • I wanted my fireflies to be randomly positioned on the screen.
  • You’ll see I create a random range for x (horizontal positioning) between 20 pixels and the width of the device minus 40 pixels.
  • For y, I did something similar, but made sure all fireflies appeared at least 200 pixels higher than the bottom of the screen.
  • Since we’re using the Skin class in the (upcoming) PlayForest screen, I needed to chose a constructor that offered an argument for skin.

This is the main game screen, where the user attempts to drag (capture) the fireflies into a jar. Some notes about this code:

  • fireflyCount was created so that each time a user came to this screen, they would randomly get somewhere between 12 and 19 fireflies — all drawn in random locations.
  • While I don’t completely understand all the ins and outs of using skin, having this in place sure seemed to make dragging to a target easier. You’ll see the code below for creating the jar, referencing the .png, and positioning it at the lower left of the screen.
  • Yes, I admit it seems strange to have the physical reference to the firefly here in this class, instead of I’m sure there’s a way to do it … but hey, losses were cut once this started working correctly. 🙂
  • The big piece here is the for loop, where we bring in a Firefly object and its arguments, populating each firefly one at a time.  We’ve also added the firefly as the main source to our DragAndDrop class, making use of the addSource method.
  • We set the payload with a String, and the actual firefly object. Since the object is getting dragged with the addSource method, we’ll also alter the firefly size, so it’s easier to see which firefly is getting dragged.
  • There are comments below discussing the color change in the jar on a drag over, as well as incrementing a variable for each successful drop (capture).

It’s fair to say I lost quite a few evenings over the course of a week, as I couldn’t figure out how to drag all of my fireflies. Creating them was easy, but making them all draggable? That was an entirely different issue.

So I brought in the big guns.

The man. The myth. The legend.

The man. The myth. The legend.

Unlike myself, Steve Rufle is an experienced programmer. Even though he’s never touched a line of Android code, let alone spent any time in LibGDX — in a matter of 30 minutes he already had the first of two possible solutions. Some of that joyous code is found above.  So a little shout-out credit to my hero this week … a few of Steve’s links:

  • And Steve, on GitHub:

Below: Here’s what PlayForest looks like, after all the fireflies populated the screen

Randome fireflies