Spam Killing

Hello JGO,

Ever since I finished my LD entry (http://www.ludumdare.com/compo/ludum-dare-30/?action=preview&uid=41601), I’ve noticed a bug where if you hold down space, it just kills the robots no matter where you are. I’ve been trying to figure out why this happens and how to fix it, but I can’t do it. Here’s my code.

[icode]Robot.java[/icode]


package com.darkcart.ld.cwld;

import java.awt.Image;
import java.util.Random;

import javax.swing.ImageIcon;

public class Robot {
	private int tileX, tileY;
	private Image robot;
	Random rand = new Random();
	Random rand1 = new Random();

	public Robot() {
		ImageIcon img = new ImageIcon("robot.png");
		robot = img.getImage();

	}

	public Image getRobot() {
		return robot;
	}

	public int getTileX() {
		return tileX;
	}

	public int getTileY() {
		return tileY;
	}

	public void placeRobot() {
		int randY = rand.nextInt(12);
		if (randY < 1) {
			randY = randY + 1;
		}

		tileX = 10;
		tileY = randY;
	}
	
	public void placeRobot1() {
		int randY = rand1.nextInt(12);
		if (randY < 1) {
			randY = randY + 1;
		}

		tileX = 10;
		tileY = randY;
	}

	public void hideRobot() {
		tileX = -1;
		tileY = -1;
	}

	public void moveForward() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tileX--;
		
	}
}

[icode]Terrain.java[/icode]


//Blah blah blah
ArrayList<Robot> listOfRobots = new ArrayList<Robot>();
Robot temp = new Robot();
public Terrain() {
		//Blah blah blah
		placeRobot();
		
	}

public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < listOfRobots.size(); i++) {
				g.drawImage(listOfRobots.get(i).getRobot(), listOfRobots.get(i)
						.getTileX() * 32, listOfRobots.get(i).getTileY() * 32,
						null);
			}
}

public void keyPressed(KeyEvent e) {
			int keyCode = e.getKeyCode();
			if (keyCode == KeyEvent.VK_SPACE) {
				temp.hideRobot();
				listOfRobots.remove(temp);
				placeRobot();
				scoreCount++;
				System.out.println(scoreCount + " robots killed");
			}
		}

Can anyone help me?

Thanks, -DarkCart

Gonna need more code than that. What is the temp variable in Terrain? (And why are you handling events in “Terrain”?)

temp.hideRobot();
listOfRobots.remove(temp);
its probably that bit there . Where is temp defined , you might be assigning it a value you dont want to. Also the reason why its spamming is because the second you press down the button the action continues to fire , you can implement a few boolean checks to stop this.

There, I added what the "temp’ variable is.


  public void moveForward() {
      try {
         Thread.sleep(500);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      tileX--; 
   }

You are accessing your data from multiple threads, leading to all kinds of race conditions.

How could I fix that?

It’s OK to access your data from multiple threads, but make sure you only update it from one thread. Also be aware that updates from one thread may not be visible on other parallel threads, unless you have marked the variables as volatile.

No, he should just not be using threads end of story.

Also I have no idea what is happening in this game, I think I have told you before to start using LibGDX. Using Thread.sleep is not how you do a game loop.