Strategies for defeating an opponent with a higher y-coordinate than the player

My goal is to create a game mechanic where if the player collides with an enemy from the side, it results in either losing a life or ending the game. However, if the player jumps on top of the enemy, the enemy will disappear.

When the player touches the enemy while they are both on the ground (player's y-axis equals the enemy's y-axis)...

player.y == enemy.y

And

When the player jumps on top of the enemy (player's y-axis is greater than the enemy's y-axis)...

player.y > enemy.y

Here is the current code I have implemented so far...

 Level.prototype.playerTouched = function(type, actor) {
      if (type == "enemy" && this.status == null && player.y == enemy.y) {
        this.status = "lost";
        this.finishDelay = 1;
      } else if (type == "enemy" && player.y > enemy.y) {
        this.actors = this.actors.filter(function(other) {
          return other != actor;
        });
      }
    };

Answer №1

If you're looking to avoid dealing with angular calculations in this scenario, then your best bet is to focus on the top and bottom positions of the colliders.

The logic is quite straightforward:

if(playercollider.originY > enemycollider.originY && playercollider.originX > enemycollider.leftX && playercollider.originX < enemycollider.rightX)
{
    //Enemy takes damage
}
else
{
    //Player takes damage
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What advantages do combined getter and setter methods offer?

In my experience, I have come across APIs that use combined getter and setter methods, particularly in scripting languages like Perl and JS used within our team. One example of this is seen in jQuery: //append something to element text var element = $(&ap ...

When clicking on a Bootstrap button, there is no visible outline

I've come across numerous examples of Bootstrap buttons where I can click on them and they display an outline. Despite my attempts to replicate this, my button doesn't show the outline as intended. What could be causing this issue? <!-- Bo ...

What is the best way to provide initial values to Field Array and manage form data?

I am facing an issue with my react select using a sortable container. The problem lies in the extracted values, which currently look like this: { "fruits": [ { "fruitName": { "id": 3, "value&qu ...

What is the best way to incorporate a loading icon onto a webpage that exclusively runs JavaScript functions?

I frequently use Ajax load icons to indicate progress during ajax requests. Is there a way to achieve the same result using regular JavaScript? For instance: $('button').on('click', function(){ showLoadingIcon(); lengthyProces ...

Issues with jQuery horizontal sliding gallery functionality

My attempt at creating a jQuery sliding video gallery is not working as I hoped. Instead of scrolling through the images when clicking arrow buttons, the entire div moves left or right depending on the direction. HTML: <div id="videocontainer"> & ...

Accordion panels refusing to collapse

I'm new to javascript and I need help with making the opened tab collapse again when the "-" sign is clicked. Here is a sample link to demonstrate: $.fn.expCollapse = function(){ $mainContainer = $(this); $mainContainer.find('.acco-he ...

Rotate images using a JQuery plugin without using HTML tags, solely through passing the images in JavaScript

I am currently working on creating a jQuery image rotator from scratch. While I know there are simple jQuery sliders/rotators available, I specifically want to include the image files directly within the JS code. I do not want to use this HTML structure: ...

Displaying information stored in a database (using Python) on a webpage using HTML

I need assistance displaying the RandomId stored in my database on my HTML page. Can someone provide guidance on how to achieve this? Below is the snippet of my code: </script> <button onclick="myFunction()"><input type="submit" value=" ...

Error 500: An invalid data type was encountered in an express.js node.js environment

Currently, I am in the process of developing an Authentication page using a combination of node.js, express.js, and mysql2. The user ID and password entered on the webpage are then passed through app.post('/login',...). However, upon submitting t ...

Implementing a switch to trigger a JavaScript function that relies on a JSON object retrieved from a GET request

Having some trouble using a toggle to convert my incoming Kelvin temperature to Celsius and then to Fahrenheit. It loads properly as default Celsius when the page first loads, but once I try toggling the function outside of locationLook, it doesn't se ...

Maximizing the width of navbar elements with Bootstrap 3

I am currently working on building a webpage with bootstrap3, but I have encountered an issue with the navigation bar. I would like the navigation links ("Web Design", "Development", "Photography", "Blog") to be evenly spaced out within the horizontal na ...

Using expressJS and MongoDB to create a secure login and registration system

I am interested in adding a login/register function to my expressJS API. Currently, I am only inserting the password and email into my database. I would like this function to first check if a user with this email is already in the database - if yes, then s ...

The Firebase Cloud Function assigned to a specific region is currently experiencing functionality issues

I'm faced with the challenge of invoking a Firebase Cloud Function that has already been successfully deployed from my Vue.js application. The specific requirement is for the function to be executed in the europe-west3 region. My approach involves cl ...

Troubleshooting: Unable to get the :last selector to work

$('.data_row:last').after('<tr class="odd"> <td class="first data" style="min-width: 29px;">2</td></tr>'); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> < ...

How to transform a nested string into a JSON object using JavaScript

I am trying to manipulate a nested query string in JavaScript. The string looks like this: var str = "( ( Sentence starts with any of null AND Sentence starts with any of null ) AND Sentence starts with any of null )" I want to split the string at the &a ...

Achieving this result in HTML using a jQuery accordion component

Hey there, I'm looking to create a full-height accordion that extends from the bottom to the top of the page, similar to what you see on this website: . I also have a footer positioned below the accordion. The content should load when the page loads ...

setting a div element to occupy a percentage of the window's height using viewport height units (vh) within the Wordpress platform

this is the desired appearance but upon pasting the identical code: <!-- placeholder div --> <div style="height:100px;"> </div> <!-- outer container div (with specified height) --> <div style="height:80vh;"& ...

What is the best method for applying comment style to text segments that may contain overlapping elements?

If I have a contenteditable div with text inside, my goal is to add comments (outside of the container div) to specific sections of the text. When the user clicks on a comment, the corresponding text segments should be styled, such as changing their color ...

There was an unexpected TypeError when attempting to access the property 'firstElementChild' of a null value

Attempting to execute the code. Steps: Click on any option --> click No, I am not sure --> Click on any option --> click Yes, lock the option The second time button (Yes, lock the option) will not perform any action, and an error was found in t ...

The error message "Required parameter not provided" appeared when trying to utilize a nested dynamic route in Next.js

Issue: The error message indicates that the required parameter (plantName) was not provided as a string in getStaticPaths for /plants/[plantName]/streaming-data/[panel] The error above is being displayed. My folder structure follows this pattern: plants & ...