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

2013/1/6

How do you make obstacles?

LonelyCreeper LonelyCreeper

2013/1/6

#
In my game I have many obstacles for the player to go around, but instead it just moves right through the object. How do I make the obstacles solid and not move-through-able?
Gevater_Tod4711 Gevater_Tod4711

2013/1/6

#
You have to make your character know that he isn't alowed to move to a position where a obstakle is placed.
public void setLocation(int x, int y) {
    if (getWorld().getObjectsAt(x, y, Obstacle.class).isEmpty()) {
        super.setLocation(x, y);
    }
}
If you change your setLocation method like this you can't move through the obstacles.
danpost danpost

2013/1/6

#
@Gevater_Tod4711, I wish it was that easy! Unfortunately, that code would still allow the images of the character and the obstacles to pass through each other as long as the image of obstacle does not intersect the center of the image of the character. Do not have time right now to explain how, but it requires one of the Actor instersect methods.
Gevater_Tod4711 Gevater_Tod4711

2013/1/6

#
Oh yes that code will not notice the image size. Ok then I'll try it again:
public void setLocation(int x, int y) {
    int oldX = getX();
    int oldY = getY();
    List<Obstacle> obstacles = getWorld().getObjects(Obstacle.class);
    super.setLocation(x, y);
    for (Obstacle obstacle : obstacles) {
        if (intersects(obstacle)) {
            super.setLocation(oldX, oldY);
            break;
        }
    }
}
I think this should work. If the object intersects the obstacle it is set back to the last location.
danpost danpost

2013/1/6

#
Easier might be:
public void setLocation(int x, int y)
{
    int oldX = getX();
    int oldY = getY();
    super.setLocation(x, y);
    if(!getIntersectingObjects(Obstacle.class).isEmpty())
    {
        super.setLocation(oldX, oldY);
    }
}
One, you do not have to import the List class; and two, you do not need to iterate through all the obstacles within the list.
LonelyCreeper LonelyCreeper

2013/1/7

#
How do I put the symbol in the act method? When I put it as setLocation(); it says it can't find symbol.
davmac davmac

2013/1/7

#
You don't need to call it, you are just overriding the default implementation. However, if you do want to call it, you must supply parameters - the x and y coordinates.
setLocation(x,y);
(where 'x' and 'y' are appropriate values).
LonelyCreeper LonelyCreeper

2013/1/8

#
How exactly do I put the code in and where?
danpost danpost

2013/1/8

#
You would have to show what code you have in that class right now.
LonelyCreeper LonelyCreeper

2013/1/8

#
okay. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; private CrabWorld crabWorld = null; private ScoreBoard scoreBoard = null; public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; } public void act() { checkKeypress(); lookForWorm(); switchImage(); } public void selectImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } public void switchImage() { if (Greenfoot.isKeyDown("up")) { selectImage(); } if (Greenfoot.isKeyDown("down")) { selectImage(); } if (Greenfoot.isKeyDown("left")) { selectImage(); } if (Greenfoot.isKeyDown("right")) { selectImage(); } } public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { turn(-6); } if (Greenfoot.isKeyDown("right")) { turn(6); } if (Greenfoot.isKeyDown("up")) { move(7); } if (Greenfoot.isKeyDown("down")) { move(-7); } } public void lookForWorm() { CrabWorld cw= (CrabWorld) getWorld(); if (canSee(Worm.class)) { eat(Worm.class); } }
davmac davmac

2013/1/8

#
Please use code tags when posting code. You don't need to put a call to setLocation(...) in. You already call move(...) which calls setLocation(...) internally.
LonelyCreeper LonelyCreeper

2013/1/9

#
so do i just put the code in the move() method?
You need to login to post a reply.