I'm currently writing code for a game that will be similar to streetfighter. So there are two players, but they should not overlap or run into each other. When they meet they should not be able to run further, because the opponent is in the way. so they have to run away or jump over. i already tried it with getobjectsInRange and if isTouching speed=0(the last night makes no sense i know. how can i do it???
here is the whole code from player 1(from player 2 is almost identical)
Translated with www.DeepL.com/Translator (free version)
import greenfoot.*; public class Fighter1 extends Actor { private int jSpeed; //Jump Speed private int rmSpeed=3; //Move Speed Right private int lmSpeed=-3; //Move Speed Lef public void act() { jump(); move(); } public void jump() { int groundLevel = getWorld().getHeight() - getImage().getHeight()/2; boolean onGround = (getY() == groundLevel); if (!onGround) // in middle of jump { jSpeed++; // adds gravity effect setLocation(getX(), getY()+jSpeed); // fall (rising slower or falling faster) if (getY()>=groundLevel) // has landed (reached ground level) { setLocation(getX(), groundLevel); // set on ground } } else // on ground { if (Greenfoot.isKeyDown("space")) // jump key detected { jSpeed = -15; // add jump speed setLocation(getX(), getY()+jSpeed); // leave ground } } } public void move() { if(Greenfoot.isKeyDown("A")) { move(lmSpeed); } if(Greenfoot.isKeyDown("D")) { move(rmSpeed); } if(isTouching(Fighter2.class)) { getObjectsInRange(100,Fighter2.class); } } }