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

2021/6/22

How do I stop my actor(Fbot) from passing through the walls?

anderslieww anderslieww

2021/6/22

#
//My wall actors are named as Wall with a capital W. public class Fbot extends Animal { public void act() { { if(Greenfoot.isKeyDown("d")) setLocation(getX()+3, getY()); if(Greenfoot.isKeyDown("a")) setLocation(getX()-3, getY()); if(Greenfoot.isKeyDown("s")) setLocation(getX(), getY()+3); if(Greenfoot.isKeyDown("w")) setLocation(getX(), getY()-3); } if(canSee(Bat.class)) { Greenfoot.playSound("air.wav"); Greenfoot.stop(); } } }
RcCookie RcCookie

2021/6/22

#
After the if statements, add
if(getX() < 0) setLocation(0, getY());
else if(getX() >= getWorld().getWidth()) setLocation(getWorld().getWidth() - 1, getY());
and vice versa.
RcCookie RcCookie

2021/6/22

#
Also please use the option „code“ below the comment editor if you want to post some code snippets, or write „{code}“ and „{/code}“ around the code part (but replace the curly brackets with brackets)
danpost danpost

2021/6/22

#
One problem is that after the movement code, it is not known which direction(s), if any, were moved in (to move back off a wall). Another is that all directions are moved in at once, which again makes telling which move causes a collision with a wall impossible. To address these issues, use local variables to hold changes in horizontal and vertical positions. Move independently along the horizontal and vertical. Check for intersecting wall after each independent move. Negate moves causing collisions.
anderslieww anderslieww

2021/6/22

#
RcCookie wrote...
After the if statements, add
if(getX() < 0) setLocation(0, getY());
else if(getX() >= getWorld().getWidth()) setLocation(getWorld().getWidth() - 1, getY());
and vice versa.
Hi, do you mean after my first if statement or second?
danpost danpost

2021/6/22

#
anderslieww wrote...
<< Quote Omitted >> Hi, do you mean after my first if statement or second?
That code (given by RcCookie) deals with world edge collisions, not wall collisions.
RcCookie RcCookie

2021/6/22

#
danpost wrote...
That code (given by RcCookie) deals with world edge collisions, not wall collisions.
Yeah that’s right
anderslieww anderslieww

2021/6/23

#
danpost wrote...
anderslieww wrote...
<< Quote Omitted >> Hi, do you mean after my first if statement or second?
That code (given by RcCookie) deals with world edge collisions, not wall collisions.
Thanks for clearing that up! So what would I need to do to stop wall collisions?
RcCookie RcCookie

2021/6/23

#
If your walls are stationary, this should work:
@Override
public void act() {
    int startX = getX(), startY = getY();

    if(Greenfoot.isKeyDown("d")) setLocation(getX() + 3, getY());
    if(Greenfoot.isKeyDown("a")) setLocation(getX() - 3, getY());
    if(Greenfoot.isKeyDown("s")) setLocation(getX(), getY() + 3);
    if(Greenfoot.isKeyDown("w")) setLocation(getX(), getY() - 3);

    if(isTouching(Wall.class)) setLocation(startX, startY);

    // ...
}
You need to login to post a reply.