public class Bobius extends Actor
{
private GreenfootImage image1 = new GreenfootImage("WalkingAnimation1.png");
private GreenfootImage image2 = new GreenfootImage("WalkingAnimation2.png");
private GreenfootImage image3 = new GreenfootImage("WalkingAnimation3.png");
private GreenfootImage image4 = new GreenfootImage("WalkingAnimation4.png");
private GreenfootImage image5 = new GreenfootImage("WalkingAnimation5.png");
private GreenfootImage image6 = new GreenfootImage("WalkingAnimation6.png");
private int frame = 5;
private int animationCount = 0;
private static final int GRAVITY = 6;
private int vSpeed;
/**
* Act - do whatever the Bobius wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeyPressed();
checkGround();
gravity();
animationCount++;
}
/**
* To check if a key is held down to move a character with extra animations
*/
private void checkKeyPressed() {
int speed = 0;
if (Greenfoot.isKeyDown("d") && Greenfoot.isKeyDown("a")) {
move(speed);
}
if (Greenfoot.isKeyDown("d")) {
move(speed + 3);
flipRight();
animateWalkingR();
counter();
}
if (Greenfoot.isKeyDown("a")) {
move(speed + 3);
flipLeft();
animateWalkingL();
counter();
}
if (Greenfoot.isKeyDown("shift") && Greenfoot.isKeyDown("d")) {
move(speed + 6);
flipRight();
}
if (Greenfoot.isKeyDown("shift") && Greenfoot.isKeyDown("a")) {
move(speed + 6);
flipLeft();
}
}
private void flipLeft() {
if (getRotation() == 0) {
turn(180);
getImage().mirrorVertically();
}
}
private void flipRight() {
if (getRotation() == 180) {
turn(180);
getImage().mirrorVertically();
}
}
/**
* The animation for the character while he walks(right)
*/
private void animateWalkingR() {
if (frame == 5) {
setImage(image1);
}
else if (frame == 10) {
setImage(image2);
}
else if (frame == 15) {
setImage(image3);
}
else if (frame == 20) {
setImage(image4);
}
else if (frame == 25) {
setImage(image5);
}
else if (frame == 30) {
setImage(image6);
frame = 5;
return;
}
frame++;
}
/**
* The animation for the character while he walks(left)
*/
private void animateWalkingL() {
if (frame == 5) {
setImage(image1);
}
else if (frame == 10) {
setImage(image2);
}
else if (frame == 15) {
setImage(image3);
}
else if (frame == 20) {
setImage(image4);
}
else if (frame == 25) {
setImage(image5);
}
else if (frame == 30) {
setImage(image6);
frame = 5;
return;
}
frame++;
}
/**
* A counter to help the animation be smoother
*/
private void counter() {
if(animationCount % 4 == 0) {
}
}
/**
* to set up gravity so the player falls on the ground and stays there
*/
private void gravity() {
setLocation(getX(), getY() + vSpeed);
}
private void checkGround() {
if (!isTouching(ground.class)) {
vSpeed += GRAVITY;
}
else {
vSpeed = 0;
}
}
}