Hi, I'm a bit stumped. I'm trying to find out a way to make a bullet fly from a spaceship.

So far, I managed to make it work but it attaches onto the coord's of my ship image(I used the position of this to map the starting position of my bullet). The problem with this is if I shoot and turn left/right while the bullet is still in the screen, it follows me.
How can I make the bullet a separate entity from the ship and somehow get the coords at the same time? I have bullets as a separate object etc.

I just can't think of a way to calculate the position of the bullet, without using the position of the ship! Or is there some way I can create a new trajectory AFTER the bullet is visible?

Don't really think I need to post code as it is not really a coding problem, more of a logical block. ha.

Thanks

I don't see why this is a problem! You fire the bullet. It has an initial position and velocity that depends on the position and velocity of the ship at that time. The bullet's position and veocity should have no connection with the ship once they are initialised - after that it just goes its own way.

I don't see why this is a problem! You fire the bullet. It has an initial position and velocity that depends on the position and velocity of the ship at that time. The bullet's position and veocity should have no connection with the ship once they are initialised - after that it just goes its own way.

Thats what I thought at first, I am maybe declaring the same variables or something somwhere, could you take a gander at my code to see if you can spot a mistake?

//Bullet class

import java.awt.*;
import java.util.ArrayList;
import javax.swing.ImageIcon;


public class Bullet {

    private int initialX, initialY;
    static boolean visible;

    private final int BOARD_WIDTH =800;
    private final int MISSILE_SPEED = 6;
    private Image image = new ImageIcon("src\\resources\\bullet.jpg").getImage();



    public Bullet(int x, int y) {
       visible = true;
        this.initialX = x;
        this.initialY = y;
        
    }
    public Bullet(){
        visible = true;
    }
    public void drawBullet(Graphics2D g){
        g.drawImage(getImage(),initialX, initialY, null);
    }

    public void moveR(){
        initialX+=MISSILE_SPEED;
        if(initialX>BOARD_WIDTH)
        {
            visible = false;
        }
    }
     public void moveL(){
        initialX-=MISSILE_SPEED;
        if(initialX<0)
        {
            visible = false;
        }
    }

    public int getY() {
        return initialY;
    }
    public int getX(){
        return initialX;
    }
    public boolean isVisible(){
        return visible;
    }
    public Image getImage(){
        return image;
    }

}

SpaceShip Class

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;


public class SpaceShip {
    Image img = new ImageIcon("src\\resources\\spaceShip.png").getImage();
    public  int  moveX, moveY,dy,dx;

    private String name = "";
    private boolean isLeft = false;
    private ArrayList bullets;

    private static int ammo;

    public SpaceShip(String iName){
        name = iName;
        ammo=100;
        bullets = new ArrayList();
        moveX = 150;
        moveY = 470;
    }
    public int getAmmo(){
        return ammo;
    }
    public void drawAmmo(Graphics2D g){

        if(getAmmo() > 0)
        {
         g.setColor(Color.WHITE);
       g.drawString("Ammo:" +getAmmo(),getX(), getY()-10);
        }
        else{
             g.drawString("NO AMMO LEFT!!",getX(), getY()-10);
 }
    }

        public Image getSpaceShipSprite()
        {
            if(isLeft == false){
                img = new ImageIcon("src\\resources\\spaceShipR.png").getImage();
            }
             else{
           img = new ImageIcon("src\\resources\\spaceShipL.png").getImage();
             }
            return img;
        
        }

        //MOVEMENT
        public void move(){
            moveX+=dx;
            moveY+=dy;
        }
        public int getX(){
            return moveX;
        }
        public int getY(){
            return moveY;
        }

     public ArrayList getBullets(){
         return bullets;
     }
     public boolean isLeft(){
         return isLeft;
     }
     public void drawShip(Graphics2D g){

            g.drawImage(getSpaceShipSprite(),getX(), getY(), null);
    }



        public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_SPACE) {
            fire();
           
        }

      if (key == KeyEvent.VK_LEFT) {
            dx = -3;
            isLeft = true;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 3;
            isLeft = false;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -3;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 3;
        }

    }

    public void fire() {
        ammo--;
        if(ammo > 0){
        if(isLeft == false)
        {
             bullets.add(new Bullet(moveX +125, moveY+28));
        }
        else{
             bullets.add(new Bullet(moveX +25, moveY+28));
        }
        }
    }

      public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }

}

Main Panel class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author andys
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.ArrayList;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;


public class GamePanel extends JPanel implements ActionListener {
    
Image bgI = new ImageIcon("src\\resources\\space.jpg").getImage(),img;
private Timer t;
boolean turnLeft = false;
private boolean inGame;
private SpaceShip spaceship;
private int B_WIDTH;
    private int B_HEIGHT;

    public GamePanel(){
         addKeyListener(new TAdapter());
        setFocusable(true);
        setSize(800,600);
        setBackground(Color.BLACK);
        inGame = true;

        spaceship = new SpaceShip("Destiny");

        t = new Timer(5,this);
        t.start();
    }

     public void addNotify() {
        super.addNotify();
        B_WIDTH = getWidth();
        B_HEIGHT = getHeight();
    }
     public void paintComponent(Graphics g) {
         super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
           g2d.drawImage(bgI,0,0,null);//Background Image
         if(inGame){

            spaceship.drawShip(g2d);
       if(spaceship.getAmmo() > 0)
       {
           spaceship.drawAmmo(g2d);       
       }
        else
        {
          spaceship.drawAmmo(g2d);
        }
            
       ArrayList<Bullet> bTs = spaceship.getBullets();
       for (int i = 0; i < bTs.size(); i++ ) {
            Bullet m = bTs.get(i);

            m.drawBullet(g2d);          
        }

         }else{
             String msg = "Game Over";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);
            g.setColor(Color.BLACK);
            g.drawRect(0, 0,800,600);
            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2,
                         B_HEIGHT / 2);
         }
          g.dispose();
          g2d.dispose();

    }
    public void actionPerformed(ActionEvent e) {
        ArrayList<Bullet> bTs = spaceship.getBullets();

        for (int i = 0; i < bTs.size(); i++) {
            Bullet m =bTs.get(i);
            
            if (m.isVisible()){
                if(spaceship.isLeft()){
                m.moveL();
                }
                else
                    m.moveR();
            }
            else bTs.remove(i);
        }
        spaceship.move();
        repaint();
    }

    private class TAdapter extends KeyAdapter {

        @Override
        public void keyReleased(KeyEvent e) {
            spaceship.keyReleased(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            spaceship.keyPressed(e);
        }
    }

}

This is bugging me so badly lol.

In your asctionPerformed you seem to be moving the bullets r/l as well as the spaceship? Is this what you intended?

In your asctionPerformed you seem to be moving the bullets r/l as well as the spaceship? Is this what you intended?

WOW ahahaha! i changed that ages ago to just have a move() method!! Forgot to bloody change the code there, how embarrassing. Thanks.

We've all don ethat at some time or another! Mark as solved?

commented: Saved me from a mental breakdown +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.