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

2015/5/13

How do I get my bluetank to move correctly and to stop going through the wall. I dont want the tank to get stuck, but to rather slightly bounce off.

thenextsuperman thenextsuperman

2015/5/13

#
import greenfoot.*; /** * Write a description of class Bluetank here. * * @author (your name) * @version (a version number or a date) */ public class Bluetank extends Actor { /** * Act - do whatever the Bluetank wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("w")) { if(!(isTouching(Wall.class)) ) move(4); else { move(-4); } } if(Greenfoot.isKeyDown("a")) { if(!(isTouching(Wall.class)) ) turn(-5); } if(Greenfoot.isKeyDown("d")) { if(!(isTouching(Wall.class)) ) turn(5); } if(Greenfoot.isKeyDown("s")) { if(!(isTouching(Wall.class)) ) move(-4); else { move(4); } } }
azazeel azazeel

2015/5/13

#
        if(Greenfoot.isKeyDown("w"))
        { 
           move(4);
           if(isTouching(Wall.class) )
                move(-4);
        }
danpost danpost

2015/5/13

#
Moving is not the only thing here that may cause a collision with a Wall object. Rotating the actor may also be a cause. Therefore rotation must also be negated if it causes a collision. The following may be closer to what is required:
public void act()
{
    int dir = 0;
    int rot = 0;
    if (Greenfoot.isKeyDown("w")) dir++;
    if (Greenfoot.isKeyDown("s")) dir--;
    if (Greenfoot.isKeyDown("d")) rot++;
    if (Greenfoot.isKeyDown("a")) rot--;
    turn(rot*5);
    if (isTouching(Wall.class)) turn(-rot*5);
    move(dir*4);
    if (isTouching(Wall.class)) move(-dir*4);
}
You need to login to post a reply.