I'm sure that it's probably something simple that I've missed, although I can't get the interaction for the platform working. The interaction with the ground works, however. Please let me know what I've done wrong.
Here's the full code for the hero.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Hero here. * * @author (your name) * @version (a version number or a date) */ public class Hero extends Bar { GreenfootImage image = getImage(); public int level; int spriteHeight = 75; // cannot use image.getHeight() as the image changes and there is no preset int spriteWidth = 75; // cannot use image.getWidth() as the image changes and there is no preset private int vSpeed = 2; // vertical speed of the hero private int hSpeed = 5; // horizontal speed of the hero private int acceleration = 1; // acceleration speed (primarily for gravity) public boolean jumping; public int jumpPower = 20; // jump strength public int frame = 1; // default frame of 1 (used for animation) public int attackFrame = 1; private int animationCounter; // used to slow the frame changes in act method public int askQuestion = 0; /** * Constructor for Hero */ public Hero() { setImage("panda-frame-3.gif"); } /** * Act - do whatever the Hero wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { controls(); isFalling(); checkNextLevel(); actAtWorldEdge(); // enemyContact(); animationCounter++; } public boolean enemyContact() { Actor wolf = getOneIntersectingObject(Wolf.class); if (wolf != null && animateAttack() == true) { return true; // add score } else { return false; // take damage } } /** * Method for controls */ public void controls() { if(Greenfoot.isKeyDown("up") && jumping == false) jump(); if(Greenfoot.isKeyDown("left")) { this.setLocation(getX() -hSpeed, getY()); if(animationCounter % 4 == 0 && touchingGround() == true || animationCounter % 4 == 0 && touchingPlatform() == true) animateLeft(); } else if(Greenfoot.isKeyDown("right")) { this.setLocation(getX() +hSpeed, getY()); if(animationCounter % 4 == 0) animateRight(); } if(Greenfoot.isKeyDown("space")) animateAttack(); } /** * Method for animation while facing right */ public void animateRight() { if (frame != 11) { setImage("panda-frame-"+frame+".gif"); frame++; } else { frame = 1; //this makes the animation start again from the beginning (frame 1) } } /** * Method for animation while facing left */ public void animateLeft() { if (frame != 11 && touchingGround() == true || frame != 11 && touchingPlatform() == true) { setImage("panda-frame-"+frame+".gif"); getImage().mirrorHorizontally(); frame++; } else { frame = 1; //this makes the animation start again from the beginning (frame 1) } } public boolean animateAttack() { if(animationCounter % 4 == 0) { if (attackFrame != 9) { setImage("panda-attack-"+attackFrame+".gif"); attackFrame++; } else { attackFrame = 1; //this makes the animation start again from the beginning (frame 1) } } if(Greenfoot.isKeyDown("space")) return true; else return false; } /** * Method for when objects of this class are not in contact with ground */ public void falling() { setLocation(getX(), getY() + vSpeed); if(vSpeed <= 18) { vSpeed = (vSpeed + acceleration) ; } jumping = true; } /** * Are objects of this class touching the ground? */ public boolean touchingGround() { Actor ground = getOneObjectAtOffset(0, spriteHeight/2, Ground.class); if(ground != null) { moveToGround(ground); return true; } else { jumping = true; return false; } } /** * Method for moving objects of this class to the ground */ public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newYG = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newYG); jumping = false; } /** * Are objects of this class touching the platforms? */ public boolean touchingPlatform() { Actor platform = getOneObjectAtOffset(0, spriteHeight/2, Ground.class); if(platform != null) { moveToPlatform(platform); return true; } else { jumping = true; return false; } } /** * Method for moving objects of this class to the ground */ public void moveToPlatform(Actor platform) { int platformHeight = platform.getImage().getHeight(); int newYP = platform.getY() - (platformHeight + getImage().getHeight())/2; setLocation(getX(), newYP); jumping = false; } /** * Are objects of this class falling? */ public void isFalling() { if(touchingGround() || touchingPlatform()) { vSpeed = 0; } else { falling(); } } /** * Booleans to test whether or not objects of this class are in contact with the edge(s) of the world */ public boolean atMinX() { if(getX() < spriteWidth/2) return true; else return false; } public boolean atMaxX() { if(getX() > getWorld().getWidth() - spriteHeight/2) return true; else return false; } public boolean atMinY() { if(getY() < spriteHeight/2) return true; else return false; } /** end of booleans for at(Min/Max)(X/Y) */ /** * Method to see if objects of this class are at the world's edges (tests the above booleans) */ public void actAtWorldEdge() { // if(atMinX() == true) // { // setLocation(getX() + hSpeed, getY()); // } // // if(atMaxX() == true) // { // setLocation(getX() - hSpeed, getY()); // } // // if(atMinY() == true) // { // setLocation(getX(), getY() + hSpeed); // } } /** * Method which explains what is done when objects of this class are jumping */ public void jump() { vSpeed = vSpeed - jumpPower; jumping = true; falling(); } /** * Check whether we should go to the next level, and if yes, start the next level */ private void checkNextLevel() { // if (getOneIntersectingObject(LevelComplete.class) != null) if (atMaxX() == true) { WorldSettings worldSettings = (WorldSettings) getWorld(); if (worldSettings.level == 1) { worldSettings.level = 2; Greenfoot.setWorld(new LevelB(this)); getWorld().removeObject(this); } else if (worldSettings.level == 2) { worldSettings.level = 3; Greenfoot.setWorld(new LevelC(this)); getWorld().removeObject(this); } else if (worldSettings.level == 3) { worldSettings.level = 4; getWorld().removeObject(this); Greenfoot.setWorld(new LevelD()); } else if (level == 4) { level = 5; getWorld().removeObject(this); Greenfoot.setWorld(new LevelE()); } else if (level == 5) { level = 6; getWorld().removeObject(this); Greenfoot.setWorld(new LevelF()); } } } }