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

2014/5/13

Making an actor unable to walk through other actors

gamer121 gamer121

2014/5/13

#
Hi, What code do I have to incorporate in my Ladybug class so that it is unable to walk through the Walls? My code is shown below. Possibly a switch statement?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Ladybug extends Actor

{   
    private static final int EAST = 0;
    private static final int SOUTH = 90;
    private static final int WEST = 180;
    private static final int NORTH = 270;
    public Ladybug() 
    {
        setRotation( Greenfoot.getRandomNumber(4) * 90 );
    }  

    public void act()

    {
        checkKeys(); 
        }

    public void checkKeys()
    {
        if( Greenfoot.isKeyDown("right") ) {
            setRotation(EAST);
            move(2);
        }
        if( Greenfoot.isKeyDown("down") ) {
            setRotation(SOUTH);
            move(2);
        }
        if( Greenfoot.isKeyDown("left") ) {
            setRotation(WEST);
            move(2);
        }
        if( Greenfoot.isKeyDown("up") ) {
            setRotation(NORTH);
            move(2);
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Wall extends Actor
{ 
    public Wall()
    {
        GreenfootImage img = new GreenfootImage(35,35);
        img.fill();
        setImage(img);
    }
    }
gamer121 gamer121

2014/5/13

#
This is my Subclass of World in case it is helpful.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class OppositeWorld extends World
{
    public OppositeWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600,600,1);
        addObject(new Ladybug(),550,50);
        addObject(new Ladybug2(),50,550);
        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Wall wall = new Wall();
        addObject(wall, 165, 198);
        Wall wall2 = new Wall();
        addObject(wall2, 219, 199);
        wall2.setLocation(200, 198);
        Wall wall3 = new Wall();
        addObject(wall3, 259, 203);
        Wall wall4 = new Wall();
        addObject(wall4, 308, 197);
        Wall wall5 = new Wall();
        addObject(wall5, 261, 279);
        Wall wall6 = new Wall();
        addObject(wall6, 332, 322);
        Wall wall7 = new Wall();
        addObject(wall7, 238, 395);
        Wall wall8 = new Wall();
        addObject(wall8, 325, 436);
        Wall wall9 = new Wall();
        addObject(wall9, 227, 490);
        Wall wall10 = new Wall();
        addObject(wall10, 323, 503);
        Wall wall11 = new Wall();
        addObject(wall11, 427, 311);
        Wall wall12 = new Wall();
        addObject(wall12, 387, 188);
        Wall wall13 = new Wall();
        addObject(wall13, 383, 116);
        wall3.setLocation(234, 198);
        wall4.setLocation(269, 198);
        wall5.setLocation(269, 232);
        wall12.setLocation(269, 267);
        wall6.setLocation(269, 302);
        wall11.setLocation(303, 303);
        wall11.setLocation(303, 302);
        wall8.setLocation(303, 336);
        wall10.setLocation(303, 370);
        wall7.setLocation(269, 370);
        wall9.setLocation(269, 404);
        wall13.setLocation(269, 437);
        Wall wall14 = new Wall();
        addObject(wall14, 273, 493);
        Wall wall15 = new Wall();
        addObject(wall15, 334, 487);
        Wall wall16 = new Wall();
        addObject(wall16, 395, 464);
        Wall wall17 = new Wall();
        addObject(wall17, 402, 421);
        Wall wall18 = new Wall();
        addObject(wall18, 409, 372);
        Wall wall19 = new Wall();
        addObject(wall19, 418, 274);
        wall14.setLocation(269, 471);
        wall15.setLocation(303, 471);
        wall16.setLocation(338, 471);
        wall17.setLocation(373, 471);
        wall18.setLocation(408, 471);
        wall19.setLocation(271, 163);
        wall19.setLocation(269, 163);
    }
}
danpost danpost

2014/5/13

#
How about an 'if' statement:
if (isTouching(Wall.class)) move(-2);
gamer121 gamer121

2014/5/13

#
Alright, thanks! Now, I want to change levels (backgrounds) every time the 2 Ladybugs intersect. I have attempted the code in my act method but with no success. Do you know how to fix this issue?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Ladybug extends Actor

{   
    private static final int EAST = 0;
    private static final int SOUTH = 90;
    private static final int WEST = 180;
    private static final int NORTH = 270;
    private int level = 0;
    public Ladybug() 
    {
        setRotation( Greenfoot.getRandomNumber(4) * 90 );
    }  

    public void act()

    {
        checkKeys(); 
        if (isTouching(Wall.class)) move(-2);
        Actor ladybug2 = getOneIntersectingObject(Ladybug2.class);
        if(ladybug2!=null) {
            Greenfoot.setWorld(new OppositeWorld2(this));
            level=1;
        }
        if(ladybug2!=null) {
            if(level==1) {
                Greenfoot.setWorld(new OppositeWorld3(this));
                level=2;
            }
            else

            if(level==2)
            {
                Greenfoot.playSound("fanfare.wav");
                level=3;
            }
        }
    }


    public void checkKeys()
    {
        if( Greenfoot.isKeyDown("right") ) {
            setRotation(EAST);
            move(2);
        }
        if( Greenfoot.isKeyDown("down") ) {
            setRotation(SOUTH);
            move(2);
        }
        if( Greenfoot.isKeyDown("left") ) {
            setRotation(WEST);
            move(2);
        }
        if( Greenfoot.isKeyDown("up") ) {
            setRotation(NORTH);
            move(2);
        }
    }
}
danpost danpost

2014/5/13

#
I think you just have your act method a little turn around. Try this:
public void act()
{
    checkKeys(); 
    if (isTouching(Wall.class)) move(-2);
    Actor ladybug2 = getOneIntersectingObject(Ladybug2.class);
    if(ladybug2!=null) {
        if(level==0) {
            Greenfoot.setWorld(new OppositeWorld2(this));
            level = 1;
        }
        else
        if(level==1) {
            Greenfoot.setWorld(new OppositeWorld3(this));
            level=2;
        }
        else
        if(level==2)
        {
            Greenfoot.playSound("fanfare.wav");
            level=3;
        }
    }
}
gamer121 gamer121

2014/5/13

#
Should the 'if (isTouching(Wall.class)) move(-2);' should go in the act method?
danpost danpost

2014/5/13

#
gamer121 wrote...
Should the 'if (isTouching(Wall.class)) move(-2);' should go in the act method?
It could go after 'checkKeys();' in the act method; but, I think it might be better placed at the end of the 'checkKeys' method itself (below and outside the last 'if' block).
You need to login to post a reply.