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

2015/12/3

Walls not working as intended

sidetracked sidetracked

2015/12/3

#
The character will hit the wall from the left side or from the top but the right and bottom side of the wall do not work and the character can walk through them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import greenfoot.*;
 
/**
 * Write a description of class Bob here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bob extends Actor
{
 
    public int speed = 3;
 
    public void act()
    {
        checkMovement();
        checkCollision();
    }   
 
    public void checkCollision() {
        if (getOneIntersectingObject(Wall.class) != null) setLocation(getX()-speed,getY()-speed);
    }
 
    public void checkMovement() {
        if (Greenfoot.isKeyDown("w")) {
            setLocation(getX(),getY()-speed);
            setImage("bobback.png");
        }
 
        if (Greenfoot.isKeyDown("s")) {
            setLocation(getX(),getY()+speed);   
            setImage("bob.png");
        }
 
        if (Greenfoot.isKeyDown("a")) {
            setLocation(getX()-speed,getY());   
            setImage("bob.png");
        }
 
        if (Greenfoot.isKeyDown("d")) {
            setLocation(getX()+speed,getY());   
            setImage("bob.png");
        }
 
    }
}
Super_Hippo Super_Hippo

2015/12/4

#
1
setLocation(getX()-speed,getY()-speed);
This does not move the character back to where it was. It moves the character 3 cells up and left.
A2O A2O

2015/12/4

#
I did it like this : int dx = 0, dy = 0; if(Greenfoot.isKeyDown("up")) { dy = -1; } if(Greenfoot.isKeyDown("down")) { dy = +1; } if(Greenfoot.isKeyDown("right")) { dx = +1; } if(Greenfoot.isKeyDown("left")) { dx = -1; } Actor w = getOneObjectAtOffset(dx, dy, Wall.class); if( w == null) { setLocation(getX()+dx, getY()+dy); } That basically sets your characters own X = dx any Y= dy when spawned and checks whether there is an Object of the class ( here ) "Wall" positioned at the spot you're currently at with your character. Only problem there is with this kind of "walls" is that you have to surround your map with the Walls otherwise u'll be able to walk into walls when you face the edge of the map, go that direction and hold for example "down", then you can walk through walls positioned underneath you. thumb rule : keep dat map surrounded by walls :D Now I'm sure there are other ways to deal with that problem, but as far as I'm fine with that way of dealing with it, I may aswell just share it ^^
danpost danpost

2015/12/4

#
WIth 'dx and 'dy' limited to the values { -1, 0, 1 }, 'getOneObjectAtOffset(dx, dy, Wall.class)' would still allow your actor to move almost half way into the walls. Try using 'getOneIntersectingObject' instead (your edge issue will be resolved as well).
sidetracked sidetracked

2015/12/4

#
thanks, I'll try these suggestions and update when I get home
You need to login to post a reply.