import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
*
*
* @author Karol
* @version 1.0
*/
public class Mouse extends Actor
{
private int speed = 7; // running speed (sideways)
private int vspeed = 0;
private int acceleration = 1;
/**
* Check keyboard input and act accordingly
*/
public void act()
{
checkKeys();
checkfall();// to check if I should fall
//Fall();
}
// @param receives no paramenters
// @return void
private void checkKeys()
{
if (Greenfoot.isKeyDown("left") )
{
setImage("Mouse_left.png");
moveLeft();
}
if (Greenfoot.isKeyDown("right") )
{
setImage("Mouse_right.png");
moveRight();
}
if (Greenfoot.isKeyDown("space"))
{
Jump();
}
}
public void checkfall()
{
if(onGround())
{
vspeed = 0;
}
else
{
Fall();
}
}
// @param receives no parameters
// returns a boolean
public boolean onGround()
{
//Actor ground = getOneObjectAtOffset ( 0, 0, Ground.class);
Actor ground = getOneObjectAtOffset ( 0, getImage().getHeight() / 2, Platform.class);
return ground != null;//!null means there is ground
}
// @param receives no parameters
// @return void
public void moveRight()
{
setLocation (getX() + speed, getY() );
}
// @param receives no parameters
// @return void
public void moveLeft()
{
setLocation (getX() - speed, getY() );
}
public void Jump()
{
vspeed = -6;
Fall();
}
public void Fall()
{
setLocation (getX(), getY() + vspeed);
vspeed = vspeed + acceleration;
}
}
