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.
1 2 3 4 5 6 7 8 9 10
| public void Shooting() { while (!SHOT) { xLaserDest = xLaserAnchor; yLaserDest = yLaserAnchor; } while (SHOT) { xLaserDest = xCoordinate; yLaserDest = yCoordinate; } } |
My MouseListener stuff:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| 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

. Please don't hesitate to ask for my entire source-code, I really want to get this thing working

-Java Newbie,
Ryan