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

2021/12/13

Creating Boundaries in Greenfoot

Ronan444 Ronan444

2021/12/13

#
I'm trying to make a game where there are walls in the level, is there a way that I could make a series of invisible walls quickly as the level has alot of walls. Also, is there a way that certain colours on a background could be set to be walls, e.g. black?
danpost danpost

2021/12/13

#
Ronan444 wrote...
I'm trying to make a game where there are walls in the level, is there a way that I could make a series of invisible walls quickly as the level has alot of walls.
It is easy to make a wall actor invisible -- just set the transparency value of its image to 0.
is there a way that certain colours on a background could be set to be walls, e.g. black?
Yes, but in most cases, that is not feasible; plus, it would require a lot more coding to accomplish. Best is to use actors. A row of connected walls can be made up of only one actor, if you want to reduce the number of actors in the world.
Ronan444 Ronan444

2021/12/15

#
Thanks, but how can I make the actors bounce off the walls?
while (isTouching(Level1Boundaries.class)) 
        {
            //setLocation(getX -5, getY());
        }
I've got this so far, but dont know how to stop the player from going through the boundary
danpost danpost

2021/12/15

#
Ronan444 wrote...
how can I make the actors bounce off the walls? << Code Omitted >>
Currently, with '5' being a literal, you cannot. It needs to be variable:
private int speed = 5;

public void act()
{
    setLocation(getX()+speed, getY());
    if (isTouching(Level1Boundaries.class))
    {
        setLocation(getX()-speed, getY());
        speed = -speed;
    }
}
Ronan444 Ronan444

2021/12/31

#
That hasn't worked unfortunately, it made the character go to the right side of the screen. Thanks for helping me btw :)
danpost danpost

2021/12/31

#
Ronan444 wrote...
That hasn't worked unfortunately, it made the character go to the right side of the screen.
Show your current codes.
Ronan444 Ronan444

2021/12/31

#
import greenfoot.*;
public class player extends Actor
{
    private int speed = 5;
    public void act()    
    {
       move();
    }
    private void move()
    {
        if (Greenfoot.isKeyDown("a")) //When "a" is pressed, the player will move to the left
        {
            setRotation(0);
                move(-4);
        }
        else if (Greenfoot.isKeyDown("d")) //When "d" is pressed, the player will move to the right
        {
            setRotation(0);
                move(4);
        }
        else if (Greenfoot.isKeyDown("w")) //When "w" is pressed, the player will move up
        {
            setRotation(270);
                move(4);
        }
        else if (Greenfoot.isKeyDown("s")) //When "s" is pressed, the player will move down
        {
            setRotation(90);
                move(4);
        }    
        setLocation(getX()+speed, getY()); //Calls the setLocation command to move the player to the previous coodinates before he was touching the wall
        if (isTouching(Level1Boundaries.class)) //If The player is touching the class "Level1Boundaries"
        {
            setLocation(getX()-dx, getY()-dy);
            setLocation(getX()-speed, getY()); //The player will be moved back from the wall
            speed= -speed; //The player will be moved back in the opposite direction to movement
        }
    }
   
}
danpost danpost

2021/12/31

#
Use the following move method:
 private void move()
{
    int dx = 0;
    if (Greenfoot.isKeyDown("left")) dx++;
    if (Greenfoot.isKeyDown("right")) dx--;
    setLocation(getX()+dx, getY());
    if (isTouching(Level1Boundaries.class))
    {
        setLocation(getX()-dx, getY());
    }
    int dy = 0;
    If (Greenfoot.isKeyDown("up')) dy =0;
    if (Greenfoot.isKeyDown("down")) dy--;
    setLocation(getX(), getY()+dy);
    if (isTouching(Level1Boundaries.class))
    {
        setLocation(getX(), getY()-dy);
    }
)
You need to login to post a reply.