Why is he not jumping?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Dante here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Dante extends Actor
{
/**
* Act - do whatever the Dante wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
static final int gravity = 2;
static final int jumpForce = 30;
int xSpeed = 4;
int ySpeed = 0;
public void act()
{
// Add your action code here.
moveHorizontal();
}
public void moveHorizontal()
{
int worldWidth = getWorld().getWidth();
int myWidth = getImage().getWidth();
int dx = 0;
if(Greenfoot.isKeyDown("a")) dx--;
if(Greenfoot.isKeyDown("d")) dx++;
setLocation(getX()+dx*xSpeed, getY());
}
private void moveVertically()
{
int worldHeight = getWorld().getHeight();
int myHeight = getImage().getHeight();
boolean onGround = false;
ySpeed += gravity;
setLocation(getX(), getY()+ySpeed);
if(getY() > worldHeight-myHeight/2)
{
ySpeed = 0;
onGround = true;
}
if(onGround && Greenfoot.isKeyDown("w"))
{
ySpeed =-jumpForce;
}
}
}
Recent Comments
2022/10/5
Jump and Run Demo w/Moving Platform
2022/10/5
Jump and Run Demo w/Moving Platform