1
\$\begingroup\$

I'm trying to get my bullet to shoot towards the mouse click but it just shoots directly towards the left from the players position here's the code i used:

Game Class (Main)

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.swing.JFrame;
public class Game extends JFrame implements KeyListener, MouseListener
{
    public Player player;
    private Image image;
    private Graphics graphics;

    public int MouseX;
    public int MouseY;

    private Font Title = new Font("SansSerif",Font.BOLD,20);
    private Font SubTitle = new Font("SansSerif",Font.BOLD,16);
    private Font Text = new Font("SansSerif",Font.PLAIN,11);


    public boolean playerUp = false;
    public boolean playerDown = false;
    public boolean playerLeft = false;
    public boolean playerRight = false;

     CopyOnWriteArrayList<Bullet> bullets = new CopyOnWriteArrayList();

     public static void main(String args[]){
        new Game();
    }

    public Game(){

        //Properties of JF
        setTitle("Ninjaa Fight!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(697,670);
        setResizable(false);
        setVisible(true);   
        addKeyListener(this);
        addMouseListener(this);
        setLocationRelativeTo(null);

        //Properties of P
        player = new Player(150,520,90,90,100, "Images/Back S..jpg");
    }
    public void paint(Graphics g){

        image = createImage(getWidth(), getHeight());
        graphics = image.getGraphics();
        paintComponent(graphics);
        g.drawImage(image, 0, 0, null);
        repaint();  
        g.drawRect(50, 50, 400, 570);
        g.setFont(Title);
        g.drawString("Ninjaa Fight!", 530, 70);
        g.setFont(SubTitle);
        g.drawString("Status", 530, 100);
        g.setFont(Text);
        g.drawString("Health: " + player.health, 530, 120);
        g.drawString("x Position: " + player.xPos, 530, 135);
        g.drawString("y Position: " + player.yPos, 530, 150);
        g.drawString("Enemies Total: " + "5", 530, 165);
        g.drawString("Enemies Defeated: " + "0", 530, 180);
        g.drawString("Enemies Left: " + "5", 530, 195);
        g.drawString("Level: " + "1", 530, 210);
        g.drawString("Player: " + "Guang Hu An", 530, 225);
        g.setFont(SubTitle);
        g.drawString("Instructions", 530, 255);
        g.setFont(Text);
        g.drawString("Up: W", 530, 275);
        g.drawString("Down: S", 530, 290);
        g.drawString("Left: A", 530, 305);
        g.drawString("Right: D", 530, 320);
        g.drawString("Shoot: L", 530, 335);
        g.drawString("Hit: K (Comming Soooon)", 530, 350);
        g.drawString("Switch Player: J (Comming Soon)", 530, 365);

        g.drawString("Credits: Areeb Raza - Designer, Coder and Programmer", 50, 640);
        g.drawString("Copyright © 2015 by Areeb Raza", 50, 655);
    }

    public void paintComponent(Graphics g){

        player.draw(g);
        player.update(this);

        for(Bullet bullet : bullets){
            bullet.draw(g);
            bullet.update(this);
        }   


    }

    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == e.VK_W){
            playerUp = true;
        }
        if(e.getKeyCode() == e.VK_S){
            playerDown = true;
        }

        if(e.getKeyCode() == e.VK_A){
            playerLeft = true; 
        }
        if(e.getKeyCode() == e.VK_D){
            playerRight = true;
        }

    }

    public void keyTyped(KeyEvent e) {

    }


    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        if(e.getKeyCode() == e.VK_W){
            playerUp = false;
        }
        if(e.getKeyCode() == e.VK_S){
            playerDown = false;
        }

        if(e.getKeyCode() == e.VK_A){
            playerLeft = false; 
        }
        if(e.getKeyCode() == e.VK_D){
            playerRight = false;
        }


    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

         Bullet playerBullet = new Bullet(-1, player.getxPos(),  player.getyPos(), 10, 10, "Images/Ninja Star.jpg");
            bullets.add(playerBullet); 

        MouseX = e.getX();
        MouseY = e.getY();
        System.out.println("(" + MouseX + ", " + MouseY + ")");

    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}

Bullet Class

import java.awt.*;
public class Bullet extends GameObject {
    private int deltaY;
    public double xVelocity;
    public double yVelocity;
    public double angle;
    public Bullet (final int deltaY, final int xPos, final int yPos, final int width, final int height,  final String img) {
        this.deltaY = deltaY;
        this.xPos = xPos;
        this.yPos = yPos;
        this.img = getImage(img);
        this.width = width;
        this.height = height;

        }


    public void draw(Graphics g) {
            g.drawImage(img, xPos, yPos, width, height, null);

        }


        void update(final Game g) {
            double bulletVelocity = 1.0; //however fast you want your bullet to travel
            //mouseX/Y = current x/y location of the mouse
            //originX/Y = x/y location of where the bullet is being shot from
             angle = Math.atan2(g.MouseX - g.player.getxPos(), g.MouseY - g.player.getyPos());
             xVelocity = (bulletVelocity) * Math.cos(angle);
             yVelocity = (bulletVelocity) * Math.sin(angle);
            xPos += xVelocity;
            xPos += yVelocity;

        }


    public int getDeltaY() {
        return deltaY;
    }

    public void setDeltaY(int deltaY) {
        this.deltaY = deltaY;
    }

    @Override
    Image getImage(String img) {
        // TODO Auto-generated method stub
        return Toolkit.getDefaultToolkit().getImage(img);
    }
    }
\$\endgroup\$
2
\$\begingroup\$

I think you have a copy/paste bug here:

xPos += xVelocity;
xPos += yVelocity;

The last line should probably be

yPos += yVelocity;
\$\endgroup\$
2
  • \$\begingroup\$ Oh yeah i'll go fix that and tell you if it worked \$\endgroup\$ – Areeb Raza Aug 14 '16 at 14:13
  • \$\begingroup\$ nope not working \$\endgroup\$ – Areeb Raza Aug 14 '16 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.