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

2018/9/1

how can i make my player jump in a different angle like 45

Mounirmoon Mounirmoon

2018/9/1

#
import greenfoot.*;
 
public class CopyOfJumper extends Actor
{
    private int ySpeed;
 
    public CopyOfJumper()
    {
    }
    public int frame = 0;
    public int waktu = 5;
    GreenfootImage gambar13 = new GreenfootImage("standinge.png");
    public void act()
    {
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            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
            }
            else if (Greenfoot.isKeyDown("right"))
      {
        move(3);
        animasi();
         
      }
      else if (Greenfoot.isKeyDown("left"))
      {
          move(-3);
          animasin();
        }
        
       else
      {
          setImage(gambar13);
        }
        }
    }
    public GreenfootImage[] imagesR = 
{
    new GreenfootImage("R1.png"),
    new GreenfootImage("R2.png"),
    new GreenfootImage("R3.png"),
    new GreenfootImage("R4.png"),
    new GreenfootImage("R5.png"),
    new GreenfootImage("R6.png"),
    new GreenfootImage("R7.png"),
    new GreenfootImage("R8.png"),
    new GreenfootImage("R9.png"),
    
};
public GreenfootImage[] imagesL = 
{
    new GreenfootImage("L1.png"),
    new GreenfootImage("L2.png"),
    new GreenfootImage("L3.png"),
    new GreenfootImage("L4.png"),
    new GreenfootImage("L5.png"),
    new GreenfootImage("L6.png"),
    new GreenfootImage("L7.png"),
    new GreenfootImage("L8.png"),
    new GreenfootImage("L9.png"),
  
};
private void animasi()
{
    frame = (frame+1)%(9*3);
    setImage(imagesR[frame/3]);
}
private void animasin()
{
    frame = (frame+1)%(9*3);
    setImage(imagesL[frame/3]);
}
          
           
}
Mounirmoon Mounirmoon

2018/9/1

#
like press space and right in the same time
VPPONAKASH VPPONAKASH

2018/9/1

#
hai
danpost danpost

2018/9/1

#
Mounirmoon wrote...
like press space and right in the same time
Easier for the user might be up and right.
Mounirmoon Mounirmoon

2018/9/2

#
i do not now how to make it
danpost danpost

2018/9/2

#
Mounirmoon wrote...
i do not now how to make it
You will use basically the same format for jumping as for firing a bullet, tracking the state of the jumping key. You will also need to retain the horizontal direction that the jump is initiated at:
/** add the field */
private int jumpX; // to retain horizontal direction jumped

/** in jumping code (initiating a jump) */
// get horizontal direction of jump
jumpX = 0;
if (Greenfoot.isKeyDown("right")) jumpX++;
if (Greenfoot.isKeyDown("left")) jumpX--;
// set initial vertical speed
ySpeed = -15;
// initiate jump (leave ground)
setLocation(getX()+4*jumpX, ySpeed++);

/** in off ground code */
setLocation(getX()+4*jumpX, ySpeed++);
You can adjust the horizontal distance jumped by altering the 4's in the setLocation calls.
You need to login to post a reply.