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

2015/11/6

Stop Hamster from going through walls

tmrpcz tmrpcz

2015/11/6

#
I am currently creating a game where a hamster has to find all the sweets in a labyrinth. the problem is that if you press two keys at the same time (i.e. up and right) the hamster can go through the walls. how can i stop that ? (you cant go through a wall if you stand right in front of it) PLEASE HELP ASAP
danpost danpost

2015/11/6

#
Either move for only one key at a time; or, determine the direction depending on all keys before moving; or, move individually for each key, checking for obstacles for each movement.
tmrpcz tmrpcz

2015/11/9

#
@danpost here is my code, what exactly do I have to change? i tried to make a code for every possibilty where two keys are pressed at the same time and then the hamster would stay at its current place but it didn't work. Please help, the due day for the game is in Wednesday !!! /*if ((Greenfoot.isKeyDown("left")&(Greenfoot.isKeyDown("up"))) ||((Greenfoot.isKeyDown("left"))&(Greenfoot.isKeyDown("right"))) ||((Greenfoot.isKeyDown("left"))&(Greenfoot.isKeyDown("down"))) ||((Greenfoot.isKeyDown("right"))&(Greenfoot.isKeyDown("up"))) ||((Greenfoot.isKeyDown("right"))&(Greenfoot.isKeyDown("down"))) ||((Greenfoot.isKeyDown("up"))&(Greenfoot.isKeyDown("down")))) { x2=x2; y2=y2; }*/ if (Greenfoot.isKeyDown("right")) { setImage("hamster2rechts.png"); wand=(Wand) getOneObjectAtOffset (1, 0, Wand.class); if (wand==null) { x2=x2+1; } } if (Greenfoot.isKeyDown("left")) { setImage("hamster2links.png"); wand=(Wand) getOneObjectAtOffset (-1, 0, Wand.class); if (wand==null) { x2=x2-1; } } if (Greenfoot.isKeyDown("down")) { setImage("hamster2unten.png"); wand=(Wand) getOneObjectAtOffset (0, 1, Wand.class); if (wand==null) { y2=y2+1; } } if (Greenfoot.isKeyDown("up")) { setImage("hamster2oben.png"); wand=(Wand) getOneObjectAtOffset (0,-1, Wand.class); if (wand==null) { y2=y2-1; } } setLocation(x2,y2);
Super_Hippo Super_Hippo

2015/11/9

#
What do you think does this do?
x2=x2;
y2=y2;
tmrpcz tmrpcz

2015/11/10

#
@Super_Hippo this was my try to say that if two keys are pressed at the same time then the hamster should stay in his position. so x and y should stay the same. but this didn't work.
danpost danpost

2015/11/10

#
Setting the value of a field to the value it already holds does not change anything. In other words, it is pointless to code the lines Super_Hippo was concerned with. The problem is that if any combination of keys are down, then one of those values may end up changed anyway because the other 'if' statements will still be executed. What you need is an 'else' after the first 'if' block and the remainder of the 'if' blocks, as a whole, between squiggly brackets so that they do not get executed when more than one key is down.
tmrpcz tmrpcz

2015/11/10

#
what exactly do i have to write as an else ? Sorry, i just started learning. Can you tell me in which line I have to put the else? And then i should delete the first lines or should i leave them ?
tmrpcz tmrpcz

2015/11/10

#
@danpost what exactly do i have to write as an else ? Sorry, i just started learning. Can you tell me in which line I have to put the else? And then i should delete the first lines or should i leave them ?
danpost danpost

2015/11/10

#
This is your code with line numbers (displayed the way you should have displayed it -- with code tags (you can click on quote to view the text as it was entered -- do not 'Post' it again, however; just look and learn):
if  ((Greenfoot.isKeyDown("left")&(Greenfoot.isKeyDown("up")))
    ||((Greenfoot.isKeyDown("left"))&(Greenfoot.isKeyDown("right")))
    ||((Greenfoot.isKeyDown("left"))&(Greenfoot.isKeyDown("down")))
    ||((Greenfoot.isKeyDown("right"))&(Greenfoot.isKeyDown("up")))
    ||((Greenfoot.isKeyDown("right"))&(Greenfoot.isKeyDown("down")))
    ||((Greenfoot.isKeyDown("up"))&(Greenfoot.isKeyDown("down"))))
{
    x2=x2;
    y2=y2;
}

if (Greenfoot.isKeyDown("right"))
{
    setImage("hamster2rechts.png");
    wand=(Wand) getOneObjectAtOffset (1, 0, Wand.class);
    if (wand==null)
    {
        x2=x2+1;
    }
}

if (Greenfoot.isKeyDown("left"))
{
    setImage("hamster2links.png");
    wand=(Wand) getOneObjectAtOffset (-1, 0, Wand.class);
    if (wand==null)
    {
        x2=x2-1;
    }
}
if (Greenfoot.isKeyDown("down"))
{
    setImage("hamster2unten.png");
    wand=(Wand) getOneObjectAtOffset (0, 1, Wand.class);
    if (wand==null)
    {
        y2=y2+1;
    }
}

if (Greenfoot.isKeyDown("up"))
{
    setImage("hamster2oben.png");
    wand=(Wand) getOneObjectAtOffset (0,-1, Wand.class);
    if (wand==null)
    {
        y2=y2-1;
    }
}
setLocation(x2,y2);
Set line 11 to the following:
else {
Indent everything from line 12 to one line before the end and insert a closing squiggly bracket, '}', there.
You need to login to post a reply.