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

2015/3/28

Help-barriers

Leenza Leenza

2015/3/28

#
I am making a game. In it I want some form of barrier that the hero can't pass through. I thought maybe I should put this line in the hero class
1
2
3
if (canSee(Barrier.class))
{
}
but I not sure what to put inside the if statement. I want it to be something like the hero will bounce off the wall/barrier if it hits the wall. However, I don't know what code would accomplish what I want it to do.
danpost danpost

2015/3/28

#
To make an object an obstacle, in basic pseudo-code, you would use something that would do this:
1
2
3
get direction to move;
move in direction determined;
if barrier hit, move back;
Leenza Leenza

2015/3/28

#
danpost wrote...
To make an object an obstacle, in basic pseudo-code, you would use something that would do this:
1
2
3
get direction to move;
move in direction determined;
if barrier hit, move back;
I did this,
1
2
3
4
5
6
7
8
9
10
11
public void lookForObstacles()
    {
        if (canSee(VerticalBarrier.class))
        {
            setLocation(getX() - 5, getY());
        }
        if (canSee(HorizontalBarrier.class))
        {
            setLocation(getX(), getY()-5);
        }
    }
But it only works when I approach the vertical bars from the left and the horizontal bars going down. How do i make it work so it works for the right and up as well?
danpost danpost

2015/3/29

#
Your 'lookForObstacles' method does not take into consideration which direction you were approaching the obstacle from. It is currently programmed to assume you approached from the left or above (I think).
Leenza Leenza

2015/3/29

#
What would the code look like if I took direction into consideration? Like how would i say if i'm approaching from the left take five away from the x position, but if I'm approaching from the right add five to the x position?
danpost danpost

2015/3/29

#
Leenza wrote...
What would the code look like if I took direction into consideration? Like how would i say if i'm approaching from the left take five away from the x position, but if I'm approaching from the right add five to the x position?
Insufficient information (code) given.
Leenza Leenza

2015/3/29

#
danpost wrote...
Insufficient information (code) given.
Ok, hopefully this is more information. I tried this code
1
2
3
4
5
6
7
8
9
10
11
12
13
public void lookForObstacles()
   {
       if (canSee(VerticalBarrier.class))
       {
           if (direction == "l")
           {
               setLocation(getX() - 5, getY());
           }
           else if (direction == "r")
           {
               setLocation(getX() + 5, getY());
           }
       }
to try and get it to take direction into consideration, but it didn't work. Why didn't it?
Leenza Leenza

2015/3/29

#
Never mind, I just found my own error. "l" and "r" needed to be switched. This is the proper code.
1
2
3
4
5
6
7
8
9
10
11
12
13
public void lookForObstacles()
   {
       if (canSee(VerticalBarrier.class))
       {
           if (direction == "r")
           {
               setLocation(getX() - 5, getY());
           }
           else if (direction == "l")
           {
               setLocation(getX() + 5, getY());
           }
       }
danpost danpost

2015/3/29

#
You cannot compare literal strings with field values like as in lines 5 and 9. You will always get a false result in the comparison as the literal String object is not the same object as the one referenced by the field. You need to compare the contents of the String objects, not the objects themselves. This is done by use of the 'equals' method. For example, line 5 should be:
1
if ("r".equals(direction))
You need to login to post a reply.