import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ditto here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Boy extends Actor
{
private GreenfootImage image1;
private GreenfootImage image2;
private GreenfootImage image3;
private GreenfootImage image4;
private GreenfootImage image5;
private GreenfootImage image6;
private GreenfootImage image7;
private GreenfootImage image8;
private GreenfootImage idle;
private int vSpeed = 0;
private int acceleration = 1;
private int frame;
private boolean isKeyPressed;
private boolean walking;
private boolean facingRight;
private boolean onGround;
public void Boy()
{
image1 = new GreenfootImage("Walk Right 1.png");
image2 = new GreenfootImage("Walk Right 2.png");
image3 = new GreenfootImage("Walk Right 3.png");
image4 = new GreenfootImage("Walk Right 4.png");
image5 = new GreenfootImage("Walk Left 1.png");
image6 = new GreenfootImage("Walk Left 2.png");
image7 = new GreenfootImage("Walk Left 3.png");
image8 = new GreenfootImage("Walk Left 4.png");
setImage(idle);
walking = false;
facingRight = true;
}
public void walkLeft()
{
int delay = 4;
walking = true;
facingRight = false;
frame ++;
if(frame < 1 * delay)
{
setImage(image5);
}
else if(frame < 3 * delay)
{
setImage(image6);
}
else if(frame < 5 * delay)
{
setImage(image7);
}
else if (frame < 7 * delay)
{
setImage(image8);
frame = 1;
return;
}
}
public void walkRight()
{
int delay = 4;
walking = true;
facingRight = true;
frame ++;
if(frame < 1 * delay)
{
setImage(image1);
}
else if(frame < 3 * delay)
{
setImage(image2);
}
else if(frame < 5 * delay)
{
setImage(image3);
}
else if (frame < 7 * delay)
{
setImage(image4);
frame = 1;
return;
}
}
public void checkKeys() { //checks different keys and performs the called actions
isKeyPressed = false;
if (Greenfoot.isKeyDown("d") && Greenfoot.isKeyDown("a"))
{
stopWalking();
isKeyPressed = true;
}
else if (Greenfoot.isKeyDown("d") && getOneObjectAtOffset( -25, 0, Ground.class ) == null) {
walkRight();
setLocation (getX()-8, getY());
isKeyPressed = true;
}
else if (Greenfoot.isKeyDown("a") && getOneObjectAtOffset( 25, 0, Ground.class ) == null) {
walkLeft();
setLocation (getX()+8, getY());
isKeyPressed = true;
}
else if (!(isKeyPressed))
{
stopWalking();
}
}
public void stopWalking()
{
walking = false;
if (facingRight)
setImage (idle);
else
setImage (idle);
}
public void act()
{
movement();
checkKeys();
}
public void movement(){
// vertical movement
vSpeed += acceleration; // gravity
setLocation(getX(), getY()+vSpeed); // move (fall or jump)
boolean onGround = false; // for current on ground state
Actor ground = getOneIntersectingObject(Ground.class); // get ground intersector
if (ground != null){ //if actor is inside ground
int direction = (int)Math.signum(vSpeed); // get vertical direction
if (direction == 1)
onGround = true; // actor is on ground
vSpeed = 0; // vertical movement is stopped
while (isTouching(Ground.class)) setLocation(getX(), getY()-direction); // move off of ground
}
if (onGround && Greenfoot.isKeyDown("w")) {
vSpeed -= 18; // jump
}
}
}
