I have a problem with my little jump and run game. I painted 6 pictures of the character to animate that he's walking. the animation works until the player reaches the point where the screen starts to scroll. The animation only doesn't work while scrolling, it's just showing one picture of the character. Has anybody an idea where the problem in my code is?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Herman is the Player
* he can move to the left or the right
* can jump and shoot
* he also can collect sandwiches to fill his lifebar
*
* @author (Lukas Hauser)
* @version (V5 16.03.15)
*/
public class Player extends Erm
{
private int hermanSpeed = 3;
GreenfootImage hermanWalk1 = new GreenfootImage ("Herman1R.png"); GreenfootImage hermanWalk7 = new GreenfootImage ("Herman1L.png");
GreenfootImage hermanWalk2 = new GreenfootImage ("Herman2R.png"); GreenfootImage hermanWalk8 = new GreenfootImage ("Herman2L.png");
GreenfootImage hermanWalk3 = new GreenfootImage ("Herman3R.png"); GreenfootImage hermanWalk9 = new GreenfootImage ("Herman3L.png");
GreenfootImage hermanWalk4 = new GreenfootImage ("Herman4R.png"); GreenfootImage hermanWalk10 = new GreenfootImage ("Herman4L.png");
GreenfootImage hermanWalk5 = new GreenfootImage ("Herman5R.png"); GreenfootImage hermanWalk11 = new GreenfootImage ("Herman5L.png");
GreenfootImage hermanWalk6 = new GreenfootImage ("Herman6R.png"); GreenfootImage hermanWalk12 = new GreenfootImage ("Herman6L.png");
public int animationCount = 1;
public int frame = 1;
GreenfootSound pickUpItem = new GreenfootSound ("PickUpItem.mp3");
boolean touchingEnemy = false;
private int movementSpeed =5;
private int fallingSpeed = 0;
private int gravity = 1;
private int jumpPower = -19;
Bullet Bullet = new Bullet();
/**
* Here we call our methods from below
* and a part of the Fall method to check if the number 3 was given to call the jump method, if not he falls
*/
public void act()
{
fireOnCommand();
hitEnemy ();
changeWorld ();
checkKeys();
GameOver();
Collect();
if(isOnGround())
{
if(user_input() == 3)
{
jump();
}
}
else
{
fall();
}
}
/**
* This Method let you collect sandwiches
*/
public void Collect()
{
Actor sandwich = getOneIntersectingObject(Sandwich.class);
if (sandwich !=null)
{
World myWorld=getWorld();
myWorld.removeObject(sandwich);
pickUpItem.play();
}
}
/**
* This method sets the Game Over screen when you fall out of the world
*/
public void GameOver()
{
World myWorld = getWorld();
if (isAtEdge())
{
GameOver gameover = new GameOver ();
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); //Adds the gameover object in the middle of the world
Greenfoot.stop();
}
}
/**
* This method checks if the Player is touching the ground
*/
public boolean isOnGround() //check if Player Touches Ground
{
//window Window = (window) getWorld();
// if(getY() + (getImage().getHeight() /2) >= (Window.getHeight() - 1))
// {
// return true;
// }
Actor ground = getOneObjectAtOffset (0, 79, Map.class);
if(ground == null)
{
return false;
}
else
{
return true;
}
}
public void jump()
{
fallingSpeed = jumpPower;
fall();
}
public void fall()
{
setLocation(getX(), getY() + fallingSpeed);
fallingSpeed = fallingSpeed + gravity;
}
/**
* This method makes a smooth jumping with gravity by pressing the up key
*/
public int user_input()
{
ScrollingWorld theWorld = (ScrollingWorld) getWorld();
Erm erm = (Erm) theWorld.getErm();
if (Greenfoot.isKeyDown("up")) //checks if the "up" key is pressed
{
return 3; //defines the number "3" for pressing the "up" key
}
return 5;
}
/**
* This is the Method for the left and right movement of the Player
* it checks if the left or right button is pressed and moves the Player left or right with an animation of the walking
*/
public void checkKeys ()
{
ScrollingWorld theWorld = (ScrollingWorld) getWorld();
Erm erm = (Erm) theWorld.getErm();
if(Greenfoot.isKeyDown("Right") && erm.shouldScroll == false) //checks if the right Key is Pressed and the Scroll method from class Erm
{
moveRight(); //moves the Player right by pressing left arrow
if (animationCount % 6 == 0) //slows the animation down
animation1(); //calls the animation method
animationCount += 1;
}
else if(Greenfoot.isKeyDown("Left") && erm.shouldScroll == false) //checks if the left Key is Pressed and the Scroll method from class Erm
{
moveLeft(); //moves the Player left by pressing left arrow
if (animationCount % 6 == 0) //slows the animation down
animation2(); //calls the animation method
animationCount += 1;
}
}
public void moveRight()
{
setLocation(getX() + movementSpeed, getY());
}
public void moveLeft()
{
setLocation(getX() - movementSpeed, getY());
}
/**
* This is the animation Method where the different pictures are set for every single Frame of the movement
* This is the animation for walking to the right side
*/
public void animation1()
{
if(frame == 1)
{
setImage (hermanWalk1);
frame = 2;
}
else if(frame == 2)
{
setImage (hermanWalk2);
frame = 3;
}
else if(frame == 3)
{
setImage (hermanWalk3);
frame = 4;
}
else if(frame == 4)
{
setImage (hermanWalk4);
frame = 5;
}
else if(frame == 5)
{
setImage (hermanWalk5);
frame = 6;
}
else if(frame == 6)
{
setImage (hermanWalk6);
frame = 1;
}
}
/**
* This is the animation Method where the different pictures are set for every single Frame of the movement
* This is the animation for walking to the left side
*/
public void animation2()
{
if(frame == 1)
{
setImage (hermanWalk7);
frame = 2;
}
else if(frame == 2)
{
setImage (hermanWalk8);
frame = 3;
}
else if(frame == 3)
{
setImage (hermanWalk9);
frame = 4;
}
else if(frame == 4)
{
setImage (hermanWalk9);
frame = 5;
}
else if(frame == 5)
{
setImage (hermanWalk10);
frame = 6;
}
else if(frame == 6)
{
setImage (hermanWalk11);
frame = 1;
}
}
/**
* This method makes the player shooting a bullet by pressing the space key
*/
public void fireOnCommand()
{
if(Greenfoot.isKeyDown("space")) //checks if the "enter" key is pressed
{
World Area = getWorld();
Area.addObject(Bullet, 0,0);
Bullet.setLocation(getX(), getY()); //adds the bullet at location from the player
}
}
/**
* This method decreases the healthbar by touching the enemy
* you can touch a enemy 4 times until the healthbar is empty, then the Gameover screen is called
*/
public void hitEnemy ()
{
Actor Enemy1 = getOneIntersectingObject (Enemy1.class);
if (Enemy1 != null)
{
World myWorld = getWorld();
ScrollingWorld scrollingworld = (ScrollingWorld)myWorld;
HealthBar healthbar = scrollingworld.getHealthBar();
if (touchingEnemy == false) //checks whether the player is touching an enemy
{
healthbar.loseHealth();
touchingEnemy = true; //makes sure that the player loses only one health touching the same Enemy
if (healthbar.health <=0)
{
GameOver gameover = new GameOver ();
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); //Adds the gameover object in the middle of the world
Greenfoot.stop();
//[myWorld.removeObject(this);]
}
}
}
else
{
touchingEnemy =false;
}
}
/**
* This method changes the World by touching the Goal Flag
*/
public void changeWorld ()
{
if(isTouching(Goal.class)) //checks if the goal class is touched
{
Greenfoot.setWorld(new Level2()); //sets a new world/level
}
}
}

