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
CrazyGamer1122 CrazyGamer1122

2012/7/30

#
I was just wondering how to make a actor jump. I have seen it done in levels but i never could figure it out.
SPower SPower

2012/7/30

#
make an instance variable:
private int gravity = 0;
in act, put this code:
setLocation(getX(), getY() -gravity);
gravity--;
this will move the Actor down forever. Let's say you want to jump when the method mustJump returns true (you can replace that of course), put this in act:
if (mustJump() ) {
   gravity = 20;
}
Does this work? If you need further assistance, just ask.
Morran Morran

2012/7/30

#
You also want to make the player stop falling when he hits the ground. A full implementation might look something like this:
public void act()
{
//fill this in with what you want,
//but add this here:
   if(mustJump())
      jump();
   checkFall();
}

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()
{
   if(onGround())
      gravity -= 20;//or choose 10 or 15 or whatever works
}
I hope this helps.
CrazyGamer1122 CrazyGamer1122

2012/7/30

#
Thanks to both of you for the help. i will post back if i need anymore help on the topic.
CrazyGamer1122 CrazyGamer1122

2012/7/30

#
I just tried out the code for mustJump().... and it says incompatible types on this part right here
if ( mustJump()  )
does it have something to do with the parenthesis?
danpost danpost

2012/7/30

#
I believe that was a bit of pseudo-code by SPower's part. You have to put in there the conditions required to trigger the jump (such as: if (onGround && Greenfoot.isKeyDown("up")) ).
CrazyGamer1122 CrazyGamer1122

2012/7/30

#
i just tried to recompile and i realized there is no method for mustJump() i am kind of new to greenfoot and i do not know what i should put in the method. i would really appreciate it.
erdelf erdelf

2012/7/30

#
@CrazyGamer1122, replace mustJump() with the code danpost provided:
onGround && Greenfoot.isKeyDown("up")
CrazyGamer1122 CrazyGamer1122

2012/7/30

#
so it would look like this?
     public void act()  
   {  
    //fill this in with what you want,  
    //but add this here:  
       if(onGround && (Greenfoot.isKeyDown("up")))  
          jump();  
       checkFall();  
  }  
      
    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()  
    {  
       if(onGround())  
          gravity -= 20;//or choose 10 or 15 or whatever works  
  }  

}
sorry if i am being bothersome, i just have not been on greenfoot in a while and i have forgotten most of the API and methods.
erdelf erdelf

2012/7/30

#
it seems right. btw you can remove line 32, you are doing the same in line 5
CrazyGamer1122 CrazyGamer1122

2012/7/30

#
thanks that took care of a major problem but i compiled and it gave me a error "could not find symbol- variable onGround" would i have to give it a new method or is there some other way to fix this? if i do need a new method please include what to type in the method. thanks for responding so quickly by the way.
erdelf erdelf

2012/7/30

#
onGround with this (), in line 5
onGround()
CrazyGamer1122 CrazyGamer1122

2012/7/30

#
okay it compiles successfully now but i was wondering if there was any thing i needed to put in the code to interact with the actor Ground?
erdelf erdelf

2012/7/30

#
for this is the method on Ground
CrazyGamer1122 CrazyGamer1122

2012/7/30

#
i tried with this as my person's code --
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")))  
          jump();  
       checkFall();  
  }  
      
    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  
  }  

}
and when i pressed the key, nothing happened to the person. am i missing something?
There are more replies on the next page.
1
2