Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Respawn moving objects on 2D array

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a 2D array grid on which a couple of runnable vehicle objects (an 'x' and an 'o') are randomly placed. The grid repeatedly prints to console, and the x/y coordinates of the vehicle objects updates on their threads which gives the impression of the vehicles animating across the grid-lanes.

However, I'm stuck on how to make the vehicle objects respawn once they've reached the end of the grid. Any ideas how I can achieve this? Here's a screen shot and the code:

Posts: 4066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a quick and dirty fix, you must change two things in the overridden run-method.

First: remove the continuation condition in the for-loop (i < g.getColumns()). As you have it now, it only runs for every column and then it stops, leaving the 'o 'and 'x' where they are.
Second: if y becomes > grid.getColumns(), reset it to 0.
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic