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

2012/7/30

How to make Jump level?

1
2
danpost danpost

2012/7/30

#
Comment out lines 28 and 29. Then, add the following at that location:
if (onGround() && Greenfoot.isKeyDown("up"))
{
    System.out.println("Actor should jump");
    System.out.println("Actor at: (" + getX() + ", " + getY() + ")");
    jump();
}
and report back with what is displayed.
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
sorry it took me a while, ive been kinda busy. but ive changed the code. my person's source looks like this (if incorrect please tell me how to fix it),
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class actor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class actor extends Actor
{
    /**
     * Act - do whatever the actor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
     
    private int gravity = 0;
    
        public void act()  
   {  
    //fill this in with what you want,  
    //but add this here:  
   if (onGround() && Greenfoot.isKeyDown("up")) 
   { 
   System.out.println("Actor should jump");  
    System.out.println("Actor at: (" + getX() + ", " + getY() + ")");  
    jump();  
       
    }
  }  
      
    private void checkFall()  
  {  
      if(!onGround())  
        fall();  
  }  
    private boolean onGround()  
  {  
       int myHeight = getImage().getHeight();  
       Actor ground = getOneObjectAtOffset(0, myHeight/2, Ground.class);  
       if(ground != null)//if the ground is really there...  
          return true;  
       //(if not, return false)  
       return false;  
  }  
    //this is quite like SPower's method  
    private void fall()  
    {  
       setLocation(getX(), getY() + gravity);  
       gravity++;//the player will accelerate as he falls  
  }  
    private void jump()  
    {  
         
          gravity -= 20;//or choose 10 or 15 or whatever works  
  }  

}
when i press the up (jump) key, nothing happens but a terminal window appears and it says "Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248)" i have uploaded the game with open source so you can get a better idea of what im talking about. try the game and please send back a fixed source for person and or ground.
davmac davmac

2012/8/2

#
Lines 39-40: if(!onGround()) fall(); As 'fall()' is the only method which will move the actor (down or up), this means that the actor won't jump if it is on the ground. Perhaps on line 33, move the actor up a bit. Alternatively, change the check on line 39 to: if(!onGround() || gravity < 0)
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
i changed line 39 as suggested and the actor still does nothing... the terminal window is still opening that says Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248) Actor should jump Actor at: (293, 248)
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
again, i uploaded what i have and you can use the source to change it.
davmac davmac

2012/8/2

#
You don't call checkFall() anywhere. You need to call it from act().
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
thanks, ill let you know what i get
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
okay, so i did that. the character actually moved up this time! there are just two problems. one-- that dang terminal window still opens. and two-- when he jumps, he does not come back down.
davmac davmac

2012/8/2

#
The terminal window opens because you output to it: System.out.println("Actor should jump"); System.out.println("Actor at: (" + getX() + ", " + getY() + ")"); Please indent your code properly. It is too difficult to read when you don't. You can use the auto-layout feature (edit menu) to do it for you. The problem now is that you only call checkFall() conditionally. The call to checkFall() should be outside the "if" statement body in the act method.
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
im sorry about the code indenting. ive deleted the output part. im not really sure how i should make the call to checkfall() outside the "if" statement. i obviously cant just write checkFall() in the source.
davmac davmac

2012/8/2

#
Sure you can. That's what you've done - you just did it in the wrong place.
public void act()
{
    // THIS IS OUTSIDE THE "IF" STATEMENT BODY
    if (...) {
       // THIS IS INSIDE THE "IF" STATEMENT BODY
    }
    // THIS IS OUTSIDE THE "IF" STATEMENT BODY
}
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
now that you put it that way.... ive just tested the code and it works perfectly! thanks to all who contributed.
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
also, do any of you do greenfoot classes because you are all really good and i have been looking for a class.
SPower SPower

2012/8/2

#
If you want to learn more about programming and about greenfoot, checkout this collection: http://www.greenfoot.org/collections/321 I can also tell you that the greenfoot book: http://www.amazon.com/Introduction-Programming-Greenfoot-Object-Oriented-Simulations/dp/0136037534 is really good.
CrazyGamer1122 CrazyGamer1122

2012/8/2

#
thank you. i have been trying to do everything by looking at the greenfoot API but it gets confusing sometimes.
You need to login to post a reply.
1
2