or 2)Not where the mouse is. Anothing thing is that java works in radians not degrees, and I have not a clue how to relate radians on a graph?(probebly could have googled: to lazy though)
please help me!
code:(mouse pressed funtion is near the bottom)
meteorD main java class
/** * @(#)meteorD.java * * meteorD Applet application * * @author * @version 1.00 2010/10/9 */import java.applet.*;import java.awt.*;import java.awt.event.*;public class meteorD extends Applet implements Runnable, MouseListener, MouseMotionListener{ //declare some variables private player gunShip; private Thread game; private projectile [] missile; //make a constant projectileSpeed = -4 private final int projectileSpeed = -4; //if game loop is running boolean running = false; //init public void init() { //new player object gunShip = new player(235,285); //10 new projectile objects missile = new projectile[10]; //mouse listener addMouseListener(this); } //create and start the thread and set running = true public void start(){ //create the thread game = new Thread(this); game.start(); running = true; } //stop the thread public void stop(){ game.stop(); if(game != null){ game = null; } running = false; } //this is supposedly called when the user exits the browser? public void destroy(){ game.stop(); if(game != null){ game = null; } running = false; } //run public void run(){ Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //while running = true while(true){ for(int i=0; i < missile.length; i++){ if(missile != null){ missile.move(); if(missile.outOfBounds()){ missile = null; } } } //clear screen and call function paint? repaint(); //try and let the thread sleep try{ game.sleep(30); } catch (InterruptedException e){ } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } //draw graphics to screen public void paint(Graphics g) { gunShip.draw(g); for(int i=0; i < missile.length; i++){ if(missile != null){ missile.draw(g); } } } public void mousePressed(MouseEvent e) { //loop through the amount of missiles for(int i=0; i < missile.length; i++){ //if one of the missiles is null if(missile == null){ //set a missile object and break from the loop missile = gunShip.fireMissile(e.getX(), e.getY()); break; } } } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { }}
player class
public projectile fireMissile(int mouseXpos, int mouseYpos); is what you want to look at
import java.awt.*;import java.applet.*;import java.math.*;public class player { //position variables private int xPos; private int yPos; //constructer public player(int x, int y){ xPos = x; yPos = y; } //set X and Y public void setX(int x){ xPos = x; } public void setY(int y){ yPos = y; } public projectile fireMissile(int mouseXpos, int mouseYpos){ projectile missile = new projectile(xPos,yPos-15); //final vector x y int Cx = xPos + mouseXpos; int Cy = yPos + mouseYpos; //final vector magnitude and angle double Cmagnitude = Math.sqrt((Cx)^2 + (Cy)^2); double Cangle = Math.atan(Cy/Cx); //send the vector angle to radians? Cangle = Math.toRadians(Cangle); //final equation for determining x velocity //found this online double CxFinal = -4*Math.cos(Cangle); //set missile objects speed missile.setX_speed( CxFinal ); missile.setY_speed( -4 ); return missile; } //drawing function public void draw(Graphics g){ g.drawOval(xPos,yPos,15,15); g.drawLine(xPos+7,yPos+7,xPos +7,yPos-5); }}
and just to be thorough here is the last java class:
projectile
import java.awt.*;import java.applet.*;public class projectile { //position variables private int xPos; private int yPos; private double speedX; private int speedY; //constructer public projectile(int x, int y){ xPos = x; yPos = y; speedX = 0; speedY = -4; } //set X and Y public void setX(int x){ xPos = x; } public void setY(int y){ yPos = y; } //set the X and Y speed of the projectile public void setX_speed(double x){ speedX = x; } public void setY_speed(int y){ speedY = y; } //move the projectile public void move(){ xPos += speedX; yPos += speedY; } //check and see if prijectile is out of screens bounds public boolean outOfBounds(){ if(yPos > 300){ return true; } if(yPos < 0){ return true; } if(xPos > 500){ return true; } if(xPos < 0){ return true; } return false; } //drawing function public void draw(Graphics g){ g.setColor(Color.red); g.drawLine(xPos+7,yPos+7,xPos +7,yPos-5); }}