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

2014/5/6

Frogger following log

kayuhnjayuhn kayuhnjayuhn

2014/5/6

#
I am working on a Frogger project and am trying to get my frog to follow the liliypad when he jumps on it. Here is the code for when he is on an car or lobster to reset to the starting point.
1
2
3
4
5
6
7
8
if (canSee(Cars.class))
       {
           setLocation(414,584);
       }
       if (canSee(lobster.class))
       {
           setLocation(414,584);
       }
I tried this to make the frog follow the lilypad, but it didn't work and I'm not sure how to make it work.
1
2
3
4
if (canSee(lilypad.class))
{
setLocation(getX(lilypad.class),getY(lilypad.class));
}
How would I code this correctly?
AIMrOrange AIMrOrange

2014/5/6

#
You cant get the x or y coordination of a class like "lilypad.class". You need to refer to an object. For example "lilypad1.getX();" getX() and getY() have no parameters.
davmac davmac

2014/5/6

#
Typically you would write something like:
1
2
3
4
Actor lily = getOneIntersectingObject(lilypad.class);
if (lily != null) {
    setLocation(lily.getX(), lily.getY());
}
However, your class names should really begin with capital letters in Java. So it should be "Lily" not "lily", and "Lobster" not "lobster".
kayuhnjayuhn kayuhnjayuhn

2014/5/9

#
Awesome thanks, and thanks for telling me that it should be capitalized, I always thought that it shouldn't because it's the first word, just to keep everything more simple, thanks again. @devmac, the way you did it worked, but seeing as I'm trying to do a frogger how the frog just attached to the lilypad and didn't unattache was an issue, I need it to be able to jump off even though it's moving with the lilypad.
davmac davmac

2014/5/9

#
You probably need to introduce some state (a variable) which tracks whether or not the frog is jumping. If it is jumping, then do not run the check for being on a Lily. Roughly:
1
2
3
4
5
6
if (isJumping == false) {
    Actor lily = getOneIntersectingObject(lilypad.class);
    if (lily != null) {
        setLocation(lily.getX(), lily.getY());
    }
}
You also need to set the variable to 'true' when the frog jumps and back to 'false' when it lands.
kayuhnjayuhn kayuhnjayuhn

2014/5/9

#
Okay thanks, I just need to find the method for the isJumping, right now I just have it in the public void act, but if I make a method for it will it work fine when I'm calling it in this code^^^?
davmac davmac

2014/5/9

#
Hmm, in my example I had 'isJumping' as a variable, not a method. Why don't you post your act() method code and I can try to help from there?
kayuhnjayuhn kayuhnjayuhn

2014/5/9

#
Here it is, thanks a ton by the way.
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
public void act()
   {
       if (Greenfoot.isKeyDown("up")) {
           setLocation(getX(), getY()-4);
       }
       if (Greenfoot.isKeyDown("down")) {
           setLocation(getX(), getY()+4);
       }
       if (Greenfoot.isKeyDown("left")) {
           setLocation(getX()-4, getY());
       }
       if (Greenfoot.isKeyDown("right")) {
           setLocation(getX()+4, getY());
       }
       if (canSee(Cars.class))
       {
           setLocation(414,584);
       }
       if (canSee(lobster.class))
       {
           setLocation(414,584);
       }
       if (Greenfoot.isKeyDown == false)
       {
           Actor lily = getOneIntersectingObject(lilypad.class); 
           if (lily != null)
           
               setLocation(lily.getX(), lily.getY()); 
           }
       }
   }
davmac davmac

2014/5/9

#
Ok, so you want a variable 'isJumping' (a boolean) and then set it to false if the frog isn't jumping, or true if it is:
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
public void act()  
   {
       boolean isJumping = false;
       if (Greenfoot.isKeyDown("up")) { 
           setLocation(getX(), getY()-4); 
           isJumping = true;
       
       if (Greenfoot.isKeyDown("down")) { 
           setLocation(getX(), getY()+4); 
           isJumping = true;
       
       if (Greenfoot.isKeyDown("left")) { 
           setLocation(getX()-4, getY()); 
           isJumping = true;
       
       if (Greenfoot.isKeyDown("right")) { 
           setLocation(getX()+4, getY()); 
           isJumping = true;
       
       if (canSee(Cars.class)) 
       
           setLocation(414,584); 
       
       if (canSee(lobster.class)) 
       
           setLocation(414,584); 
       
       if (isJumping == false
       
           Actor lily = getOneIntersectingObject(lilypad.class);   
           if (lily != null)  
           {   
               setLocation(lily.getX(), lily.getY());   
           }  
       
   }  
kayuhnjayuhn kayuhnjayuhn

2014/5/9

#
OH MY GOSH THANK YOU SO MUCH! It worked great! Thanks a ton!
You need to login to post a reply.