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

2013/5/6

code for little crab game

vbh vbh

2013/5/6

#
i want to make it possibel for the crab to hide behind another object, and make sure the lobster cant eat it when its hidding, i use the lookForCrab metod from the book. Any suggestion to how i can make the code???
Gevater_Tod4711 Gevater_Tod4711

2013/5/6

#
Your lobster always has to check whether there is a crab he can eat. If he finds one he eats it. But if you use a if condition in the eat method the crab'll be able to be protected by some objects. Therefore you need to declare a method in your crab which checks whether the crab is protected at the moment:
//in your crab class;
public boolean isProtected() {
    return getOneIntersectingObject(Protector.class) != null;
}
//instead of Protector you should use the classname of the object with which you want to crab to protected.
//in the eat method of your lobster class;
public void eat(Class clss) {
Actor actor = getOneObjectAtOffset(0, 0, clss);
    if(actor != null) {
        if (actor instanceof Crab && !((Crab) actor).isProtected()) {
            getWorld().removeObject(actor);
        }
        else {
            getWorld().removeObject(actor);
        }
    }
vbh vbh

2013/5/6

#
public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { if (actor instanceof Crab && !((Crab) actor).isProtected()) { getWorld().removeObject(actor); } else { getWorld().removeObject(actor); } } is actor the crab, the lobster or my hide object?
Gevater_Tod4711 Gevater_Tod4711

2013/5/6

#
The variable actor can be every subclass of actor. So it could be all of them. But when you check whether it is a Crab (actor instanceof Crab) you can be shure if this is true it is a crab. And if this crab is protected it doesn't get eaten.
vbh vbh

2013/5/6

#
Mouse = crab Cat = lobster Stamme = name of object to hide behind i have wrote this code but now my cat dont eat the mouse at all ? public void lookForMouse(Mouse mouse) { Actor Mouse = getOneObjectAtOffset(0,0, Mouse.class); if(Mouse != null) { if(Mouse instanceof Mouse && !((Mouse)Mouse).isProtected()) { getWorld().removeObject(Mouse); } else { getWorld().removeObject(Mouse); } } }
Gevater_Tod4711 Gevater_Tod4711

2013/5/6

#
If you just look for the mouse you can do it like this:
public void lookForMouse() { 
Mouse mouse = (Mouse) getOneObjectAtOffset(0,0, Mouse.class);
    if(mouse != null && !mouse.isProtected()) {
        getWorld().removeObject(mouse); 
    }
}
If this doesn't work probably your isProtected method is wrong. In this case you should post it here so I can tell you what's wrong with it.
vbh vbh

2013/5/6

#
first mistake i didnt call the method in act lookForMouse(Mouse mouse); but when i do i get this message ')' expected
Gevater_Tod4711 Gevater_Tod4711

2013/5/6

#
Probably you have forgot a breket in your method. Could you post the code?
vbh vbh

2013/5/6

#
in cat (lobster class) call in act: lookForMouse(); public void lookForMouse() { Mouse mouse = (Mouse) getOneObjectAtOffset(0,0, Mouse.class); if(mouse != null && !mouse.isProtected()) { getWorld().removeObject(mouse); } } in mouse( crab klasse) call in act: isProtected(); public boolean isProtected() { return getOneIntersectingObject(Stamme.class) != null; }
vbh vbh

2013/5/6

#
sry wrong code
vbh vbh

2013/5/6

#
its realy not working for me now lol it was the right code stamme is the object to hide behind
vbh vbh

2013/5/6

#
got it now its working thank you very much for u help
You need to login to post a reply.