[Javascript] How do I make a hitbox?

  • Context: Java 
  • Thread starter Thread starter lionely
  • Start date Start date
  • Tags Tags
    Javascript
Click For Summary

Discussion Overview

The discussion revolves around creating a hitbox for a 2-D side scroller game using JavaScript, specifically within the context of Khan Academy's programming environment. Participants are addressing issues related to collision detection and game mechanics.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant shares a code snippet for collision detection but reports that the character moves through objects without triggering the game over condition.
  • Another participant suggests breaking the problem down into simpler components, specifically by creating a function to check rectangle intersections and testing various use cases.
  • A participant expresses concern about starting over and questions what is specifically wrong with their code.
  • Another participant points out that the original code is difficult to read and notes that the collision checking does not loop through the positions array or use the correct boundaries.
  • One participant proposes an alternative approach of using spherical objects for collision detection based on distance to the center, suggesting it may reduce frustration related to hitbox boundaries.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the specific issues with the original code, and multiple viewpoints on how to approach the problem remain. There is a mix of suggestions and concerns about the current implementation.

Contextual Notes

Some participants note that the code snippet provided is incomplete, which may limit the ability to diagnose the problem accurately. There are also references to specific boundaries and collision detection methods that are not fully resolved.

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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 28 ·
Replies
28
Views
4K
Replies
4
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K