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

2017/6/6

If actor touches wall, go back to start

alishap alishap

2017/6/6

#
I am making a maze game, how can I make my actor go to the start (specific x and y coordinates) when they touch the wall (which in this case is a png black outline under actor not world)
Super_Hippo Super_Hippo

2017/6/6

#
I am not sure what you mean with the last ( ) part. Is the wall an actor or the background of the world? If it is an actor, it is simply this (in act method of your actor):
1
if (isTouching(Wall.class)) setLocation(specificX, specificY);
alishap alishap

2017/6/6

#
Sorry I should have been more clear, the wall is an actor. It's a PNG file if that matters. I tried your code, however now I am unable to move my actor anymore.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void act()
    {
        movement();
        checkWalls();
    }
    public void movement()
    {
        if(Greenfoot.isKeyDown("right")) move(3);
        turn(90);
        if(Greenfoot.isKeyDown("down")) move(3);
        turn(90);
        if(Greenfoot.isKeyDown("left")) move(3);
        turn(90);
        if(Greenfoot.isKeyDown("up")) move(3);
        turn(90);
    }
   public void checkWalls()
   {
    if (isTouching(Maze1.class)) setLocation(481, 360);
    }
Super_Hippo Super_Hippo

2017/6/6

#
So your Maze1 is one actor representing the whole maze? That's like the same as having the PNG as the background of the world. This makes it more difficult. You need to access the image and check if the pixels at the actors position represent a wall (=have the wall color). Or maybe if it is a grid-like maze, you could save an int array in your world showing where the walls are and then, you could check where in the world your actor is and see if there is a wall.
alishap alishap

2017/6/6

#
I think despite being a PNG, Greenfoot is taking the entire image to account and that is the problem. Is there any way to change this? Maybe I could have it be the world instead of actor and get the colour, so when that colour is touched setLocation.
alishap alishap

2017/6/6

#
Yes, the Maze1 represents the whole maze. If it's easier I could just have the PNG as the background of the world.
Super_Hippo Super_Hippo

2017/6/6

#
It doesn't matter if it's the background or an actor, so having the actor is pointless. Yes, Greenfoot's intersection methods do not look for transparency. You can't change this, you can only write a method which checks the color of the pixel where the actor is. (Or using an array representing the map as suggested.)
alishap alishap

2017/6/7

#
Okay, I will do that. Thank you so much!
You need to login to post a reply.