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

2019/11/7

I need help with my code again. I can't get my actor to jump at all.

jjc289 jjc289

2019/11/7

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class player extends Mover
{ 
    private int vSpeed = 0;
    private int jumpStrength= 30;
    private static final int acceleration = 2;
    
    /**
     * Act - do whatever the player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeys();
       gameOver();
       checkFire();
       checkFall();
       win();
    }    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("right"))
        {
           moveLeft(); 
        }
        if (Greenfoot.isKeyDown("left"))
        {
           move(-5); 
        }
        if (Greenfoot.isKeyDown("space") && onGround())
        {  
          jump();  
        }
    }
    public void setVSpeed(int speed)
    {
     vSpeed = speed;   
    }
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
        vSpeed=vSpeed+acceleration;
        
    }
    public void gameOver()
    {
       if ( isTouching(Blackline.class) )
       {
           setLocation(44, 580);
       }
    }
    public void checkFire()
    {
       if ( isTouching(fire.class) )
       {
         Greenfoot.playSound("au.wav");
       }
    }
      public void jump()
    {
        setVSpeed(-jumpStrength);
        fall(); 
    }
    public void checkFall()
    {
      if (onGround())
      {
        fall();  
      }
    }
    public void win()
    {
       if (isTouching(Finish.class))
       {
           setLocation(44, 584);
           Greenfoot.playSound("fanfare.wav");
           Greenfoot.stop();
       }
    }
  
^ this is player class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Mover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mover extends Actor
{
    private int vSpeed = 0;
    private static final int speed = 10;
    public void moveRight()
    {
      setLocation ( getX() - speed, getY() );
    }
    public void moveLeft()
    {
      setLocation ( getX() + speed, getY() ); 
    }
    
    public boolean onGround()
    {
      Object under = getOneObjectAtOffset(0,getImage()
      .getHeight()/2-8,Start.class);
      return under !=null;
    }
    public boolean onGround2()
    {
      Object under = getOneObjectAtOffset(0,getImage()
      .getHeight()/2-8, safezone.class);
      return under !=null;
    }
    public boolean onGround3()
    {
      Object under = getOneObjectAtOffset(0,getImage()
      .getHeight()/2-8, safezone.class);
      return under !=null;
    }
    public boolean onGround4()
    {
      Object under = getOneObjectAtOffset(0,getImage()
      .getHeight()/2-8, safezone.class);
      return under !=null;
    }
    public boolean onGround5()
    {
      Object under = getOneObjectAtOffset(0,getImage()
      .getHeight()/2-8, safezone.class);
      return under !=null;
    }
    public boolean onGround6()
    {
      Object under = getOneObjectAtOffset
      (0,getImage().getHeight()/2-8, finishblock.class);
      return under !=null;
    }
}
danpost danpost

2019/11/7

#
Your checkFall method at line 71 in your player class seems wrong -- if on ground, then fall? fall to where?
jjc289 jjc289

4 days ago

#
Okay, he doesn't fall through the ground now, he moves just fine, but still wont jump.
danpost danpost

4 days ago

#
jjc289 wrote...
Okay, he doesn't fall through the ground now, he moves just fine, but still wont jump.
What is your updated codes?
jjc289 jjc289

4 days ago

#
I got him to jump, but he continuously jumps now.
jjc289 jjc289

4 days ago

#
 /**
     * Act - do whatever the player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeys();
       gameOver();
       checkFire();
       checkFall();
       win();
    }    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("right"))
        {
           moveLeft(); 
        }
        if (Greenfoot.isKeyDown("left"))
        {
           move(-5); 
        }
        if (Greenfoot.isKeyDown("space"))
        {  
          jump();  
        }
    }
    public void setVSpeed(int speed)
    {
     vSpeed = speed;   
    }
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
        vSpeed=vSpeed+acceleration;
        
    }
    public void gameOver()
    {
       if ( isTouching(Blackline.class) )
       {
           setLocation(40, 530);
       }
    }
    public void checkFire()
    {
       if ( isTouching(fire.class) )
       {
         Greenfoot.playSound("au.wav");
       }
    }
      public void jump()
    {
        setVSpeed(-jumpStrength);
        fall(); 
    }
    public void checkFall()
    {
      if (onGround()||onGround2()||onGround3()||onGround4()||onGround5()
      ||onGround6())
      {
        jump();  
      }
      else{
       fall();    
      }
    }
Super_Hippo Super_Hippo

4 days ago

#
In your checkFall-method, you say that it should jump whenever it is on ground. The only time the jump-method should be called is in line 25. Replace line 62 with this:
vSpeed = 0;
You need to login to post a reply.