Java [Javascript] How do I make a hitbox?

  • Thread starter Thread starter lionely
  • Start date Start date
  • Tags Tags
    Javascript
AI Thread Summary
The discussion revolves around a user developing a 2-D side-scrolling game in JavaScript using Khan Academy's compiler. The main issue highlighted is that the character moves through objects without triggering a game-over condition upon collision. Participants suggest breaking down the problem by creating a function to check for rectangle intersections and testing various collision scenarios. They emphasize the importance of encapsulating code into smaller, manageable functions to improve readability and functionality. Specific feedback indicates that the user's collision detection code lacks proper looping through the positions array and may not be using the correct boundaries for collision checks. Additionally, a suggestion is made to simplify collision detection by using spherical objects, which could enhance gameplay by reducing frustration related to hitbox inaccuracies.
lionely
Messages
574
Reaction score
2
Alright I'm trying to make a game using Java-Script, it is a 2-D side scroller

if ( xPositions>this.x && xPositions<this.x+50 && yPositions>y && yPositions<y+50) {
y-= 5;
fill(255, 255, 255, 127);

stroke(0, 0, 0, 127);

rect(80, 110, 280, 150, 15);

noStroke();
fill(0, 0, 0);

textFont(loadFont("Times", 36), 36);

text("Game Over\n Try again!", 100, 150);



text("Final Score: " + points, 85, 250);
noLoop();
}

Also it might be different from usual Java-Script because I am using Khan academy's compiler.
The problem is that my character just moves through the objects and nothing happens, when the object touches my character the game should end. I have no idea what to change.

So could someone please help me?
 
Technology news on Phys.org
It's hard to tell exactly what's wrong since you only showed a portion of the code. I can tell from this snippet that you are not encapsulating things as well as you could be.

I would try to break it down into a simpler problems.

First, write a function that does only one thing: It checks if two rectangles intersect. Then write tests to check that all the use cases (bottom right edge with top left edge, non-overlapping, exactly overlapping, etc) return the correct result. Then try incorporating that function into the rest of your program.

Ideally your code should be broken out into functions that are small, have few responsibilities and are easy to verify on their own.

Maybe you could link to the khan academy problem you are working on so we can give better guidance?
 
Last edited:
oh Okay thank you
 
But what exactly is wrong with mine?? I don't really want to start over the programming :(
 
Why is that it wouldn't work with mine? Mine is supposed to check if the ellipses intersect with my bubble's hit box.
 
Your code is kind of hard to read, but I see a few things:
You are not looping through the positions array during the check.
Your collision checking code does not seem to be using the correct boundries.
 
I thought had the correct boundaries ,my collision check is the final if statement
 
Last edited:
  • #10
I really like that website. I might have to start sending more people there. I thought it was just for maths.

I would suggest an improvement, make the objects you are dodging/collecting all spherical. Then you can test for collision by distance to the centre. This would alleviate player rage issues caused by extending the hitbox past the boundaries of the player object.
 
Last edited:
Back
Top