Painting problem & Creating more than 1 "enemy" -Fixed/Answered-

Hello! First off, I got this problem. I am currently using a drawLine to create a “laser”. I have an “anchor-point” set up in the middle of my character (at the moment a box). Whenever I click, it shoots and when I release it goes back to the middle. However, if I move, it still points to my previous location from when I released the button. Here’s the code:

This was intended as a stop-gap to see if I could fix it.

public void Shooting() {
		while (!SHOT) {
			xLaserDest = xLaserAnchor;
			yLaserDest = yLaserAnchor;
		}
		while (SHOT) {
			xLaserDest = xCoordinate;
			yLaserDest = yCoordinate;
		}
	}

My MouseListener stuff:


class Mouse extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent e) {
			int xCoordinate = e.getX();
			int yCoordinate = e.getY();
			xMouse = xCoordinate;
			yMouse = yCoordinate;

			xLaserDest = xMouse;
			yLaserDest = yMouse;

			SHOT = true;
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			xLaserDest = xChar + 16;
			yLaserDest = yChar + 16;

			SHOT = false;

		}
	}

My “run” method:


public void run() {
		try {
			while (true) {
				FPS.tick();
				move();
				zom.FindCharacter();
				Thread.sleep(10);
			}
		} catch (Exception e) {
			System.out.println("Error");
		}

	}

and finally my “Paint” and “PaintComponent” methods:


public void paint(Graphics g) {
		dbImage = createImage(getWidth(), getHeight());
		dbg = dbImage.getGraphics();
		paintComponent(dbg);
		g.drawImage(dbImage, 0, 0, this);

	}

	public void paintComponent(Graphics g) {

		xLaserAnchor = xChar + 16;
		yLaserAnchor = yChar + 16;

		g.drawImage(backImage, 0, 0, this);
		g.drawImage(charImage, xChar, yChar, this);
		g.drawImage(zombieImage, zom.xZombie, zom.yZombie, this);

		g.setColor(Color.YELLOW);
		g.setFont(font2);
		g.drawString(Controls, 90, 40);

		g.drawString("MoveSpeed: " + moveSpeed, 480, 40);

		g.setColor(Color.YELLOW);
		g.setFont(font);
		g.drawString("FPS: " + FPS.FPS, 5, 40);

		g.setColor(Color.YELLOW);
		g.drawLine(xLaserAnchor, yLaserAnchor, xLaserDest, yLaserDest);

		repaint();
	}

Another thing I want to do is create more than 1 “Zombie”. I do not know how to do this at my current level, and Google hasn’t helped much on this issue. If you could help me out here, I’d be eternally grateful :D. Please don’t hesitate to ask for my entire source-code, I really want to get this thing working :stuck_out_tongue:

-Java Newbie,
Ryan

Nothing looks wrong, x and y LaserDest seem to be assigned just fine when you press the mouse.

Try making them volatile? You make them volatile by specifying “volatile” right their declaration.

The entire source could be useful :slight_smile:

As for creating more zombies, it is simply a matter of using OO to your advantage. Create a Zombie class and then have an ArrayList of Zombies then you loop through to update and draw :slight_smile:

Okay! I just want to say first that I don’t know how to go about creating the ZombiesArray. Is there any chance you have an example for me to study? Also, here’s my code in Pastebin: http://pastebin.com/7cnJ4Xf7. Thanks in advance for help :smiley:

I ran your code and it runs fine, except for when I move with the arrows keys, it draws a line from the old location to the new location. This is because you don’t update x and y LaserDest when then anchor points move. Using the mouse shoots fine every time.

BTW- A JFrame isn’t meant to be extended, you should be extending JComponent and adding that to a JFrame. Then you don’t have to create a new image every time, just simply draw in that JComponent’s paintComponent(Graphics)

Thanks for your help! I got most of the stuff fixed. Now I just have to look up a few bits and pieces. Thanks again :stuck_out_tongue: