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

2014/5/19

Objects getting stuck at walls and edges?

Tommy99 Tommy99

2014/5/19

#
Hello. I am having a problem with my "OppositeWorld" game. Every time any of the two Ladybugs has a wall in front of it, it cannot move. I just wanted it to be so the ladybugs cannot move forward if there is a wall in front of it. Here is the code for the Ladybugs superclass: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Ladybugs extends Actor { protected static final int EAST = 0; protected static final int SOUTH = 90; protected static final int WEST = 180; protected static final int NORTH = 270; private int counter = 0; /** * Moves the object one cell in the direction it is facing */ public boolean canMove() { return !facingWall() && !facingEdge(); } public boolean facingEdge() { switch(getRotation()){ case EAST: return getX()==getWorld().getWidth()-1; case WEST: return getX()==0; case NORTH: return getY()==0; case SOUTH: return getY()==getWorld().getHeight()-1; } return false; } public boolean facingWall() { int xOff = 0, yOff = 0; switch(getRotation()){ case EAST: xOff=1; break; case SOUTH: yOff=1; break; case WEST: xOff=-1; break; case NORTH: yOff=-1; break; } return getOneObjectAtOffset(xOff, yOff, Wall.class)!=null; } } Here is the code for the Ladybug1 class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Ladybug1 extends Ladybugs { private int counter = 0; public Ladybug1(int size) { getImage().scale(size,size); setRotation(0); } public Ladybug1() { this(40); } public void act() { if(canMove()){ checkKeys(); //moves forward one cell } } public void checkKeys() { if(Greenfoot.isKeyDown("left")){ setRotation(180); counter++; if(counter==5){ move(1); counter=0; } } if(Greenfoot.isKeyDown("right")){ setRotation(0); counter++; if(counter==5){ move(1); counter=0; } } if(Greenfoot.isKeyDown("up")){ setRotation(270); counter++; if(counter==5){ move(1); counter=0; } } if(Greenfoot.isKeyDown("down")){ setRotation(90); counter++; if(counter==5){ move(1); counter=0; } } } } Here is the code for the Ladybug2 class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Ladybug2 extends Ladybugs { private int counter = 0; public Ladybug2(int size) { getImage().scale(size,size); setRotation(180); } public Ladybug2() { this(40); } public void act() { if(canMove()){ checkKeys(); //moves forward one cell } } public void checkKeys() { if(Greenfoot.isKeyDown("left")){ setRotation(0); counter++; if(counter==5){ move(1); counter=0; } } if(Greenfoot.isKeyDown("right")){ setRotation(180); counter++; if(counter==5){ move(1); counter=0; } } if(Greenfoot.isKeyDown("up")){ setRotation(90); counter++; if(counter==5){ move(1); counter=0; } } if(Greenfoot.isKeyDown("down")){ setRotation(270); counter++; if(counter==5){ move(1); counter=0; } } } } Thank you for your help and time.
danpost danpost

2014/5/19

#
For one thing, it is kind of useless to check if 'canMove' when afterwards, the checkKeys' method may change the direction. Use 'checkKeys' first only to orient the ladybugs, then ask if 'canMove' (and move if ok) afterwards.
Tommy99 Tommy99

2014/5/19

#
It works, but I want to make it so the ladybugs move when I press the arrow keys. Right now, they are moving by themselves and I am only changing their directions.
danpost danpost

2014/5/19

#
You should use the code you have above, except change all occurrences of 'if(counter==5)' to 'if(counter==5 && canMove())'. Also, remove the condition on calling 'checkKeys' in the act method:
1
2
3
4
public void act()
{
    checkKeys();
}
danpost danpost

2014/5/19

#
You may get some weird facing actions with the way the movement is coded. You could end up moving one way while facing another if two direction keys are pressed simultaneously. Putting 'else's between the different direction checks may help; but then, if the first direction is not a valid one, the second one will not be checked..\ I would do the facing part first, before the moving part.
You need to login to post a reply.