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

2021/1/29

How to rotate an actor away from another actor??

Har8mes Har8mes

2021/1/29

#
Hi, I'm currently writing a program where a Lobster is chasing a Crab and I'm stuck currently getting the Crab rotate a random amount away from the Lobster. Here is the Part I've got some problems... PS: Note that i'm not finished with the I've put in.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void isatEdge()
   {
      if (isAtEdge()) {
          if (!getWorld().getObjects(Lobster.class).isEmpty())
          {
             Actor player = (Lobster)player.getX(0);
             int playerX = player.getX();
             int playerY = player.getY();
          }
      } else {
          move(-50);
          turn(45 - Greenfoot.getRandomNumber(90));
      }
   }
RcCookie RcCookie

2021/1/29

#
Well if you want to turn away from an actor you can use
1
2
3
Actor other = someWayToGetTheActor();
turnTowards(other.getX(), other.getY());
turn(180);
So if you want to turn away with 90 degrees of randomness, you can replace the last line with this:
1
turn(135 + Greenfoot.getRandomNumber(90));
Har8mes Har8mes

2021/1/29

#
Thnx for the reply, It could sound stupid but what do i put in "someWayToGetTheActor();" because i get an error??
Roshan123 Roshan123

2021/1/29

#
Har8mes wrote...
Thnx for the reply, It could sound stupid but what do i put in "someWayToGetTheActor();" because i get an error??
(xyz)getOneInsertingObject(xyz.class);
Har8mes Har8mes

2021/1/29

#
So I put in the code but it doesn't work:
1
2
3
4
5
6
7
8
9
10
public void dodgeLobster()
    {
        if (!getWorld().getObjects(Lobster.class).isEmpty())
           {
              Actor other = getOneIntersectingObject(Lobster.class);
              turnTowards(other.getX(), other.getY());
              turn(180);
        }
    }
}
Har8mes Har8mes

2021/1/29

#
That is fixed but i have still a problem at line 6. When i start my program it says: java.lang.NullPointerException at Crab.dodgeLobster(Crab.java:53) at Crab.act(Crab.java:15)
Roshan123 Roshan123

2021/1/29

#
Actor other = (Lobster)getOneIntersectingObject(Lobster.class);
Roshan123 Roshan123

2021/1/29

#
This may work
1
2
3
4
5
6
7
8
9
10
11
public void dodgeLobster()
    {
Actor other = getOneIntersectingObject(Lobster.class);
        if (!getWorld().getObjects(Lobster.class).isEmpty() && other!=null)
           {
               
              turnTowards(other.getX(), other.getY());
              turn(180);
        }
    }
}
Har8mes Har8mes

2021/1/29

#
Either i missed something or the code still doesn't work. I get an error code that something is wrong with "turnTowards(other.getX(), other.getY());"
1
2
3
4
5
6
7
8
9
10
public void dodgeLobster()
    {
        if (!getWorld().getObjects(Lobster.class).isEmpty())
           {
              Actor other = (Lobster)getOneIntersectingObject(Lobster.class);
              turnTowards(other.getX(), other.getY());
              turn(180);
        }
    }
}
Roshan123 Roshan123

2021/1/29

#
Roshan123 wrote...
This may work
1
2
3
4
5
6
7
8
9
10
11
public void dodgeLobster()
    {
Actor other = (Lobster)getOneIntersectingObject(Lobster.class);
        if (!getWorld().getObjects(Lobster.class).isEmpty() && other!=null)
           {
               
              turnTowards(other.getX(), other.getY());
              turn(180);
        }
    }
}
Paste it and not the above one I forgot to change line 3
danpost danpost

2021/1/29

#
1
2
3
4
5
6
7
8
9
public void dodgeLobster()
{
    Actor lobster = getOneIntersectingObject(Lobster.class);
    if (lobster != null)
    {
        turnTowards(lobster.getX(), lobster.getY());
        turn(135+Greenfoot.getRandomNumber(91));
    }
}
Har8mes Har8mes

2021/1/29

#
ok nvm what i replied I tested it 3 times but why does it teleport so weird??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Crab extends Actor. I've put my whole code in this messge so you can comprehend my code:
{   int AmountEatenPizza = 0;
    int PizzaLeft = 7;
    public void act()
    {
       move(10); 
       eatPizza();
       isatEdge();
       VictoryCondition();
       dodgeLobster();
    
    public void eatPizza()
    {
      if(isTouching(pizza.class)) {
         removeTouching(pizza.class);
         Greenfoot.playSound("slurp.wav");
         AmountEatenPizza = AmountEatenPizza + 1;               
         PizzaLeft = PizzaLeft - 1;
         int x = Greenfoot.getRandomNumber(500);
         int y = Greenfoot.getRandomNumber(1000);
         getWorld().addObject(new pizza(), x,y);
         System.out.println("You've eaten "+ AmountEatenPizza + " much pizza!!");
         System.out.println("There is " + PizzaLeft + " Pizza left to eat!!");
      }
    }
    public void isatEdge()
    {
       if (isAtEdge()) {
           move(-10);
           turn(Greenfoot.getRandomNumber(90) + 135);
           } else {
           move(10);
           turn(Greenfoot.getRandomNumber(90) + 135);
       }
    }
    public void  VictoryCondition()
    {
        if (AmountEatenPizza == 7) {
            System.out.println("You've won!!!");
            Greenfoot.stop();
        }
    }
    public void dodgeLobster()
    {
          Actor lobster = getOneIntersectingObject(Lobster.class);
          if (lobster != null)
          {
              turnTowards(lobster.getX(), lobster.getY());
              turn(135+Greenfoot.getRandomNumber(91));
          }  
    }
}
danpost danpost

2021/1/29

#
Insert before turnTowards:
1
move(-10);
to get off lobster. Remove lines 31 thru 33.
Har8mes Har8mes

2021/1/29

#
Now It's working as it was planned!!! Thank you guys for your help!!!!
You need to login to post a reply.