This site requires JavaScript, please enable it in your browser!
Greenfoot back
TimQ1704
TimQ1704 wrote ...

2022/4/2

Greenfoot Actors not touch each other?

TimQ1704 TimQ1704

2022/4/2

#
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);  
        }
    }
}
AbdulrazaqAS AbdulrazaqAS

2022/4/3

#
Try this
public void move()
    {
        if(!isTouching(Fighter2.class)){
            if(Greenfoot.isKeyDown("A"))
            {
                move(lmSpeed);
            }
            if(Greenfoot.isKeyDown("D"))
            {
                move(rmSpeed);
            }
        }
    }
AbdulrazaqAS AbdulrazaqAS

2022/4/3

#
This one will be better because you can move in the opposite side relative to the touch.
public void move()
    {
        if(!isTouching(Fighter2.class)){
            if(Greenfoot.isKeyDown("A"))
            {
                move(lmSpeed);
            }
            if(Greenfoot.isKeyDown("D"))
            {
                move(rmSpeed);
            }
        } else if(isTouching(Fighter2.class))
        {
            if(getOneObjectAtOffset(getImage().getWidth(), 0, Fighter2.class) != null) //Checks left
            {
                if(Greenfoot.isKeyDown("A"))
                {
                    move(lmSpeed);
                }
            } else if(getOneObjectAtOffset(-getImage().getWidth(), 0, Fighter2.class) != null) //Checks right
            {
                if(Greenfoot.isKeyDown("D"))
                {
                    move(rmSpeed);
                }
            }
        }
    }
Super_Hippo Super_Hippo

2022/4/4

#
I’d do something like this:
int dx=0;
if (Greenfoot.isKeyDown("A")) dx-=lmSpeed;
if (Greenfoot.isKeyDown("D")) dx+=rmSpeed;
if (dx != 0)
{
    setLocation(getX()+dx, getY());
    if (isTouching(Fighter2.class)) setLocation(getX()-dy, getY());
}
AbdulrazaqAS AbdulrazaqAS

2022/4/5

#
some corrections
public void move()
    {
        if(!isTouching(Fighter2.class)){
            if(Greenfoot.isKeyDown("A"))
            {
                move(lmSpeed);
            }
            if(Greenfoot.isKeyDown("D"))
            {
                move(rmSpeed);
            }
        } else if(isTouching(Fighter2.class))
        {
            if(getOneObjectAtOffset(getImage().getWidth()/2, 0, Fighter2.class) != null) //Checks left
            {
                if(Greenfoot.isKeyDown("A"))
                {
                    move(lmSpeed);
                }
            } else if(getOneObjectAtOffset(-getImage().getWidth()/2, 0, Fighter2.class) != null) //Checks right
            {
                if(Greenfoot.isKeyDown("D"))
                {
                    move(rmSpeed);
                }
            }
        }
    }
You need to login to post a reply.