Results 1 to 6 of 6
Thread: Turret tank movement
- 03-26-2011, 05:54 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
-
Hello and welcome to the forum. Unfortunately I doubt if any one can provide you with any helpful advice until we know a lot more about your current program, your current problem, and what you've tried to do to solve the problem and how it's not working. Usually the more specific the problem and the more useful information presented in the question, the better the answer.
Please see the link in my signature below on how to ask smart questions as it will provide you with useful tips on how to give us the information we need to begin to be able to help you.
Much luck.
-
you'll need to read up on this:
How to Write a Mouse-Motion Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
to track the mouse position. then simply set the position of your turret according to the mouse position
- 03-26-2011, 07:39 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
This is all i have right now. I am using MVC architecture for this program.package guiexample;
import java.awt.MouseInfo;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.audio.*;
class TankController1 implements KeyListener, MouseListener, MouseMotionListener {
private Tank tank = null;
private TankView view = null;
int n, check = 0;
double w, h;
public void keyPressed(KeyEvent e) {
System.out.println(tank.ra);
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
tank.ra += 0.1;
if (tank.ra < 2 * Math.PI) {
System.out.println(tank.ra);
tank.ra = 360;
}
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
tank.setVy(0);
tank.ra -= 0.1;
if (tank.ra < 2 * Math.PI) {
System.out.println(tank.ra);
tank.ra = 360;
}
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
tank.setVy(tank.getVy() + 0.2);
System.out.println("y= " + tank.getY());
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
tank.setVy(tank.getVy() - 0.2);
}
public Tank getTank() {
return tank;
}
public void setTank(Tank tank) {
this.tank = tank;
}
public TankView getView() {
return view;
}
public void setView(TankView view) {
this.view = view;
}
public void mouseMoved(MouseEvent e) {
/* System.out.println("y= " + tank.getY());
System.out.println("m y= " + MouseInfo.getPointerInfo().getLocation().y);
System.out.println("m x= " + MouseInfo.getPointerInfo().getLocation().x);*/
//throw new UnsupportedOperationException("Not supported yet.");
}
public void doUpdateGameLogic() {
tank.setY(tank.getY() + tank.getVy());
}
}
- 03-26-2011, 08:06 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-26-2011, 09:44 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
This is for the laser on my tank. The line still follows the mouse even if it's out of the JFrame. Is there another way of getting the x and y location of the mouse or am i doing anything wrong?Java Code:g.setColor(Color.red); g.drawLine((int) (tank.getX() + 15), (int) (tank.getY() + 14), (MouseInfo.getPointerInfo().getLocation().x), MouseInfo.getPointerInfo().getLocation().y);
Similar Threads
-
Chess Game - Movement
By danborgir in forum Advanced JavaReplies: 1Last Post: 03-14-2011, 11:45 AM -
Key/mouse movement
By falkon114 in forum New To JavaReplies: 3Last Post: 02-21-2011, 02:12 AM -
artificial intelligence for a tank
By newbie79 in forum New To JavaReplies: 9Last Post: 12-22-2010, 04:27 PM -
Sprite Movement
By Curtiz in forum Java GamingReplies: 1Last Post: 04-26-2010, 01:31 PM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 03:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks