Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9098941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:29:17+00:00 2026-06-17T00:29:17+00:00

I am working on making a Top-Down Shooter Clone in Java. After an hour

  • 0

I am working on making a Top-Down Shooter Clone in Java. After an hour or so, I was able to figure how to position the ship’s projectile which is a laser relative to the direction of the ship’s sprite. Problem is, if I move the ship, the projectile follows relative to the ship direction when I move the ship. As you can see this is a problem because the laser should be moving independently regardless of where the ship is when it is fired.

Here’s the code which presents my problem:

private Image ship;
    private int ship_dx = 500;
    private int ship_dy = 400;
    private int ship_velocity = 5;
    private boolean isLaser = false;
    private static final int laser_DyOffSetOfBall = 48;
    private static final int laser_DxOffSetOfBall = 23;
    private Image laser;
    private int laser_dx = ship_dx+laser_DxOffSetOfBall;
    private int laser_dy = ship_dy-laser_DyOffSetOfBall;
    private int laser_velocity= 10;

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(ship, ship_dx, ship_dy, this);
        if(isLaser){

            laser_dy -= laser_velocity;
            laser_dx = ship_dx+laser_DxOffSetOfBall;
            g.drawImage(laser,laser_dx,laser_dy,this);
            if(laser_dy < 50)
            {
                isLaser = false;
                laser_dy = ship_dy-laser_DyOffSetOfBall;
            }
        }
            Toolkit.getDefaultToolkit().sync();
    }

Here’s the game!:

IMG

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:29:18+00:00Added an answer on June 17, 2026 at 12:29 am

    You only need to set the x location of the laser on the first time it is painted, because otherwise on every repaint, the laser moves horizontally relatively to the ship. To do this you can add another boolean isFirst (for the first paint of the laser), that is set to true when isLaser first turns to be true, and the x location is only set when isFirst is true.

    boolean isFirst = true;
    
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(ship, ship_dx, ship_dy, this);
        if(isLaser){
            laser_dy -= laser_velocity;
            if(isFirst){
    
                laser_dx = ship_dx+laser_DxOffSetOfBall;
                isFirst = false;
    
            }
            g.drawImage(laser,laser_dx,laser_dy,this);
            if(laser_dy < 50)
            {
                isLaser = false;
                laser_dy = ship_dy-laser_DyOffSetOfBall;
            }
        } else {
            isFirst = true;
        }
            Toolkit.getDefaultToolkit().sync();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on making a simple drawing program. So far I am able
I am working on making some drop-down menus. Our designer prescribed a gap between
I'm working on making my first Python C extension, which defines a few functions
I am working on making a book catalog searchable using Solr. I have written
I'm working on making a script with node.js for setting up my dzen2 in
I'm working on making 3 VM versions of XP with IE6, 7, and 8
I'm working on making an ASCII based game, and everywhere I look people are
I am working on making a new SQLite database. Obviously for a number of
I am working on making a twitter client app. One of the requirement of
I am working on making user pages for a certain type of user who

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.