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

2014/3/25

Regarding Chasing

iOptical iOptical

2014/3/25

#
Hey guys, Im really new to Greenfoot with an assignement due soon.... I was wondering, I want one actor of mine to chase another actor Lets say for example I want "Boo" to chase "Slimy", what do I need to do in order to make that happen.
lordhershey lordhershey

2014/3/25

#
give Boo a reference to Slimy and have Boo do something like this.
1
2
3
4
5
void act(){
    //this is Boo's act method
    turnTowards(Slimy.getX(),Slimy.getY());
    move(5);
}
this will cause Boo to turn toward Slimy and chase after him like a homing missile. This is the basic gist of it. You might need help learning how to set a reference , and also note Slimy has to be added to the world otherwise that routine would throw an exception.
iOptical iOptical

2014/3/25

#
Hey thank for the reply Im getting "act() in Boo cannot override act() in greenfoot.Actor attempting to assign weaker access privielges; was public" If thats a referencing error, how can I fix it, reaaaally new at this... I do apologize.
danpost danpost

2014/3/25

#
iOptical wrote...
Im getting "act() in Boo cannot override act() in greenfoot.Actor attempting to assign weaker access privielges; was public"
That just means that you did not use 'public void act()', but 'private' or 'protected'. The act method must be made 'public'.
iOptical iOptical

2014/3/25

#
Oh yeah I figured it out, thanks After doing so , its telling me "Non-static method getX() cannot be referenced from a static context" Any idea?
danpost danpost

2014/3/25

#
iOptical wrote...
After doing so , its telling me "Non-static method getX() cannot be referenced from a static context"
Yeah, that is the referencing issue mentioned above. You cannot call 'getX' on a Class (the Slimy class); you have to call it on an object of that class (one that was created from the Actor subclass 'Slimy'). If you only have one Slimy object in the world, then
1
Actor slimy = (Actor)getWorld().getObjects(Slimy.class).get(0);
will get a reference to the actor provided it is in the world. Then you can use 'slimy.getX()'.
iOptical iOptical

2014/3/25

#
Where exactly do i paste that code Thanks for the fast reply, much appreciated.
iOptical iOptical

2014/3/25

#
Couldnt paste it in both Boo's editor or Slimy's, so not sure.
iOptical iOptical

2014/3/25

#
Here is my Slimy Code
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class Slimy extends Actor
{
    int value = 4;
    int x,y;
    int counter = 0;
    int speed = 0;
     
     
    public void act()
    {
        x = getX();
         
        move();
        y = getY();
               if(Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            move(value);
        }
         
        if(Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            move(value);
        }
         
        if(Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            move(value);
        }
         
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(value);
        }
         
        if(isTouching(RedMushroom.class))
       {
           removeTouching(RedMushroom.class);
            
        //Add 1 to the counter
           counter++;
           ((Score)getWorld().getObjects(Score.class).get(0)).ScoreCounter(counter);
           Greenfoot.playSound("Slurp.wav");
        }
         
          if(isTouching(GreenMushroom.class))
       {
           removeTouching(GreenMushroom.class);
            
        //Add 1 to the counter
           counter++;
           ((Score)getWorld().getObjects(Score.class).get(0)).ScoreCounter(counter);
           Greenfoot.playSound("Slurp.wav");
        }
         
        if (getWorld() instanceof Level1)
        if(counter>11)
        {
            Greenfoot.setWorld(new Level2());
        }
         
        if (getWorld() instanceof Level2)
        if(counter>23)
        {
            Greenfoot.setWorld(new Level3());
        }
         
        if (isTouching(Boo.class))
        {
       Greenfoot.setWorld(new GameOverWorld());
    }
 
 
        
         
         
    }
     
    private void move()
    {
        // get change in rotation
        int dz = 0;
        if (Greenfoot.isKeyDown("right")) dz++;
        if (Greenfoot.isKeyDown("left")) dz--;
        // apply change in rotation
        setRotation(getRotation() + dz * 5);
        // get change in speed
        int ds = -1;
        if (Greenfoot.isKeyDown("up")) ds += 20;
        // adjust speed
        speed += ds;
        if (speed < 0) speed = 0;
        if (speed > 2000) speed = 2000;
        // and move
        if (speed >= 200) move(speed / 100);
    }
}
and the Boo code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class Boo extends Slimy
{
     
 public void act()
 
     
    turnTowards(Slimy.getX(),Slimy.getY());
    move(5); 
    
}
iOptical iOptical

2014/3/25

#
Any help, really urgent.
iOptical iOptical

2014/3/25

#
Any1 could help me please? This is the only thing thats setting me back.... Its due in 2 hours
iOptical iOptical

2014/3/25

#
I got it to work, it was just a matter of capitilized letters in ur code Actor slimy = (Actor)getWorld().getObjects(Slimy.class).get(0); Actor Slimy was correct Thanks Danpost
danpost danpost

2014/3/25

#
Actually, since by convention, field and variable names should begin with lowercase letters, you should change 'Slimy.getX()' to 'slimy.getX()' and similar for 'getY()' instead of changing 'Actor slimy' to 'Actor Slimy'.
iOptical iOptical

2014/3/25

#
Last question Dan Is there away where I add a timer to each world which makes everything pause and when the timer is at 0 the world starts, kinda like a countdown to start.
danpost danpost

2014/3/25

#
iOptical wrote...
Is there away where I add a timer to each world which makes everything pause and when the timer is at 0 the world starts, kinda like a countdown to start.
Yes, however it would not be an easy task if the world was to stay visible. You could (1) (a) add a int startTimer field to your world class (b) if timer' is not zero, no actor can be allowed to act - enclose all Actor subclass 'act' method code in an 'if' with the condition stated (c) first thing in world act method, if timer is not zero, run timer down and exit the method (2) (a) import my PauseWorld support class (b) add a timer, as in (1) (a and c) above that counts down (to return to your main world when zero) (c) start the Pauseworld world from your main world constructor Suggestion (2) does not require you do anything to your Actor classes and your main world will be 'frozen' until returned to from the PauseWorld world.
You need to login to post a reply.