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 6543021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:17:46+00:00 2026-05-25T11:17:46+00:00

Long story short, I’m making a racing game in Java. I’m self-taught using some

  • 0

Long story short, I’m making a racing game in Java. I’m self-taught using some books and my knowledge of math logic and I’ve only been programming for three weeks so I’m still learning the ins and outs of it all. Here’s some background:

I’ve got a player image surrounded by a bounding rectangle and the code will check to see when that player rectangle intersects a rectangle that is the finish line. Each time it successfully intersects the line, p1Laps gets incremented. When the value reaches a certain point, the game is over and the player is declared the winner.

Here’s the problem and the questions:
My problem is that Java is counting multiple intersects each time the rectangles cross. Usually 8 intersects so p1Laps gets incremented 8 times. This would not be a problem if it happened consistently, but sometimes the laps increment at different values. I’ve encountered increments of 4,7, and 8 so it’s hard to set a value to ensure that the race will end after a certain number of laps.

My first question is “why?” Why is java counting so many intersects when the two rectangles cross? I’m assuming it has something to do with them both being 2D shapes, but I could be wrong.

My second question is how to make the increments happen at a consistent value? Preferably “1” but that is not paramount as I can just adjust the finishing value.

Here is the code that seems relevant (a lot of code is removed):

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import java.applet.AudioClip;

public class RacerDoom extends JFrame {
//lap counter
int p1Laps=0;

//bouding rectangles
Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30);
Rectangle finishtop = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);

//check for intersect
if(p1.intersects(finishtop)&&p1Direction==UP){
                        p1Laps++;}
//choose winner
if(p1Laps>=24) {
                        if(!winnerChosen) {
                            winnerChosen = true;
                            break;
}
}

As stated, the increments usually go up by 8, but I’ve had them go by 7 for (seemingly) no reason and they go up by only 4 if “Boost” is enabled (doubles the speed of player).
Thanks for your help.

  • 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-05-25T11:17:47+00:00Added an answer on May 25, 2026 at 11:17 am

    The reason is probably that it takes a few frames for the car to move across the finish line. You can solve this by keeping track of whether each car already has intersected the finish line. Use a boolean variable for each car, e.g.

    boolean p1IsOnFinishLine = false;
    boolean p2IsOnFinishLine = false;
    

    Modify your if to check whether the car intersects the finish line and p1IsOnFinishLine is false – this means that it is the first frame in which the car hits the line. If so, increase p1Laps and set p1IsOnFinishLine to true. Now, in the next frame, the car will still intersect the finish line, but since p1IsOnFinishLine is true, you know that you already have counted that lap, so you don’t need to do it again. When the car no longer intersects the finish line, you can set p1IsOnFinishLine to false, so that you’re ready for the next crossing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Long story short, the database I'm using needs to get looked at. Until that
Long story short, I'm making a custom Swing component that's basically a JTable with
Long story short, client's hosting is using php 5.2.5 and i desperately need to
Long story short, VSS decided I wasn't allowed to have some code changes. I
Long story short, I'm using a buggy WordPress template and we're too far into
Long story short, because of some issues with architecture and the fact that someone
To cut a long story short I am attempting to add some functionality to
Long story short, I am developing a simple game, and I have a Game
Long story short, I'm creating a graph using networkx and the lower the weights
Long story short: I have some controller logic that requests a value from the

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.