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

2017/2/24

jumping help pls

ninjaFI ninjaFI

2017/2/24

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

/**
 * Write a description of class CopyOfsmaller here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CopyOfsmaller extends Actor
{ 
    GifImage myGif = new GifImage("ezgif.com-resize.gif");
    private int ySpeed;
    private int apexTimer;
    public Jumper()
    {
    }
    /**
     * Act - do whatever the CopyOfsmaller wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      setImage( myGif.getCurrentImage() );
       int speed = 3;
      
      if(Greenfoot.isKeyDown("left"))
      setLocation(getX() - speed, getY());
      if(Greenfoot.isKeyDown("right"))
      setLocation(getX() + speed, getY());
      
      
      int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            if (ySpeed == 0 && apexTimer > 0) apexTimer--; // run apex timer
            if (ySpeed == 0 && apexTimer > 0) return; // apex timer still running
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
           }
        }
        else // on ground
        {
            if ("space".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
                apexTimer = 100;  // set apex timer (adjust value to suit)
            }
    }
 }
}
invalid method declaration:return type required please help
Super_Hippo Super_Hippo

2017/2/24

#
Remove lines 14-16. It seems like you renamed the class but forgot to rename the constructor. Since the constructor is empty, you can also just remove it. (Or name it the same as the class.)
ninjaFI ninjaFI

2017/2/24

#
thx
You need to login to post a reply.