Hello! There is a code for colliding two objects and a 16x16 texture. The implementation is such that a player jumping onto the enemy from above kills him, and the x coordinate of the enemy kills the player ... Using the Rectangle class, I do a collision check and then I write such conditions inside.

This condition for the death of the player:

if((rectPlayer.x + rectPlayer.width < arrayEnemy.get(num).getRect().x + arrayEnemy.get(num).getRect().width || rectPlayer.x + rectPlayer.width > arrayEnemy.get(num).getRect().x + arrayEnemy.get(num).getRect().width) && rectPlayer.y < arrayEnemy.get(num).getRect().y + arrayEnemy.get(num).getRect().height .... тут какой либо код... } 

This condition is for a player to hit the enemy from above:

 if((rectPlayer.x + rectPlayer.width < arrayEnemy.get(num).getRect().x + arrayEnemy.get(num).getRect().width || rectPlayer.x + rectPlayer.width > arrayEnemy.get(num).getRect().x + arrayEnemy.get(num).getRect().width) && rectPlayer.y > arrayEnemy.get(num).getRect().y + arrayEnemy.get(num).getRect().height .... тут какой либо код... } 

My code on the x coordinate works ... but it’s worth connecting this player cannot kill the enemy from above ... and the player dies for some reason ... or vice versa, if you remove the condition x, the player jumps to the enemy without problems .... what's my mistake?

    1 answer 1

    Hello. Honestly, I would never understand by code what you want to do. Here is my suggestion for implementing the actions you need:

     if (rectPlayer.overlaps(arrayEnemy.get(num).getRect())) { if (rectPlayer.y + rectPlayer.height > (arrayEnemy.get(num).getRect().y + arrayEnemy.get(num).getRect().height)) { // Код для убийства существа } else { // Код для убийства Player } 

    }

    The idea is that there is a check on the collision of objects, then to the side where the hero came from. I did not check the code, I hope PS: In your example, the coordinates are likely to be confused, as well as the conditions with OR should be enclosed in brackets.

    • Thanks for the answer ... but your code does not work ... the brackets are superfluous and -2 is superfluous ... I have a comparison with exact dimensions ... I will think further ... thanks for answering ... - dev3java
    • Try conjuring overlaps (), maybe I forgot how to use it correctly, libGDX hasn't worked for a long time. I just had a similar project, everything worked well. - Maxim
    • I changed your code ... now it works for sure ... thanks for the help! - dev3java
    • @AndreyKonstantinovich you would at least leave a comment for this while editing - Alexey Shimansky
    • @ Alexey Shimansky during editing and left a comment. What is the problem then? - dev3java