Light Racer 2.0 - Day 10 - Trail Drop Item

Today I added a new item which, when picked up, causes the racer's trail at that point to fade to nothing. The idea is that the game will be more dynamic if new opportunities can open up while playing. My tests of the item show that it's going to be a good addition to the game as I've already enjoyed playing the game more with it. The code itself was a little annoying because the entire game was designed with the concept of 1 trail (List of Coordinates to represent the path) per player and now I had to change it to multiple trail segments for each player. To complicate matter more, each trail segment could be "dissolving" independently. I solved all of these problems though and by 2:15am, the item was done.

Here's what I did on Day 10:

Changing list of points for trail of player into a list of trail segments. Segments now handle their own removal state and redraw state
AI and drawing code has been updated to handle multiple segments
Added a method to the Player: dropTrail()
Added a working TrailDropItem

I added a very simple method to the Player class called "dropTrail()." This method is short and sweet. It works by "capping off" the current trail and then calling the remove method, which sets a flag for the update() method. Each trail segment has its own update method that controls if it's removing or removed. The Player still draws the trails, though I could probably change it so that the trail is responsible for drawing itself entirely.

In the Player class:

private void updateTrails(LightRacerWorld world) {
ArrayList trails = this.trails;
for (int i = 0; i < trails.size(); i++) {
TrailSegment trail = trails.get(i);
if (!trail.isPathActive) {

public void dropTrail() {
currentTrail.path.add(new Coordinate(curX, curY));
currentTrail = new TrailSegment(gameResources, curX, curY);

Here's my new TrailSegment class

public class TrailSegment {
public ArrayList path = new ArrayList(100);
public boolean isPathActive = true;
public boolean redrawPaths = false;
public int pathRemovalFrameNumber = -1;

private long pathRemovalTime = 0;
private long nextPathRemovalFrameTime = 0;
private boolean remove = false;

private GameResources gameResources;

public TrailSegment(GameResources gameResources, int initialX, int initialY) {
this.gameResources = gameResources;
path.add(new Coordinate(initialX, initialY));

public void update(LightRacerWorld world) {
// if we're in a path-removing state
if (remove) {
// if we haven't yet begun the removal, keep taking time off the clock
if (pathRemovalTime > 0) {
pathRemovalTime -= world.tickDelta;
// if we've counted down to removal, start
if (pathRemovalTime <= 0) {
// if this is the first removal update, play the sound
if (pathRemovalFrameNumber == -1) {
// if there is still time left until the next frame, take time off the clock
if (nextPathRemovalFrameTime > 0) {
nextPathRemovalFrameTime -= world.tickDelta;
// if the time to next frame has counted down, increment or finish up.
if (nextPathRemovalFrameTime <= 0) {
if (pathRemovalFrameNumber == 4) {
pathRemovalFrameNumber = -1;
nextPathRemovalFrameTime = 0;
isPathActive = false;
remove = false;
} else {
nextPathRemovalFrameTime = PATH_REMOVAL_FRAME_DELAY;
redrawPaths = true;

* Causes this trail segment to remove after the delay
* @param delay
public void remove(int delay) {
remove = true;
pathRemovalTime = delay;

I drew up the image in photoshop. I had imagined a sort of "overload" or electrical cut-off of some sort that would cause the trail to break, so I came up with this round-shaped arcing thingy. I think I'm going to make all of the items rounded and slightly 3d-looking, but for now, this will have to do.

I designed this in photoshop and created 3 frames with varying yellow "electrical" lines.

Trail Drop Item in PhotoshopTrail Drop Item in Photoshop

Here's how it looks in-game:

And finally a video demonstration:


Post a comment here or discuss this and other topics in the forums


ArrayList trails = this.trails;

why did you do this?

On devices without a JIT,

On devices without a JIT, caching field accesses is about 20% faster than repeatedly accessing the field.

See for more info.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.