Ok now the animations work great, though i still have the problem of him teleporting into nowhere.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This class is just an example. You can delete it or change the code.
* It's not necessary for the scrolling system.
*/
public class Azure extends ScrollingActor
{
private int vSpeed = 0;
private int acceleration = 1;
private int jumpStrength = 20;
private static GreenfootImage[] standAnim = new GreenfootImage[9];
private static GreenfootImage[] rightAnim = new GreenfootImage[11];
private static GreenfootImage[] leftAnim = new GreenfootImage[11];
private static GreenfootImage[] standleftAnim = new GreenfootImage[9];
private static GreenfootImage[] runAnim = new GreenfootImage[10];
private static GreenfootImage[] runleftAnim = new GreenfootImage[10];
static
{
for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_0"+(i+1)+".png");
for (int i=0; i<standAnim.length; i++) standleftAnim[i] = new GreenfootImage("StandLeft_0"+(i+1)+".png");
for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("Walk-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
for (int i=0; i<rightAnim.length; i++) leftAnim[i] = new GreenfootImage("WalkLeft-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
for (int i=0; i<standAnim.length; i++) runAnim[i] = new GreenfootImage("Run_0"+(i+1)+".png");
for (int i=0; i<standAnim.length; i++) runleftAnim[i] = new GreenfootImage("RunLeft_0"+(i+1)+".png");
}
private int animCount; // counter for animations
private int animDelay = 6; // initially set for standing animation (frame rate field)
private void setAnim(GreenfootImage[] anim, int frameRate)
{
animSet = anim;
animCount = -1; // prepares to set first image when incremented
animDelay = frameRate;
animate(); // calls a method to set a frame of this newly set animation
}
private GreenfootImage[] animSet = standAnim;
/**
* Here you can tell your actor what he has to do.
*/
public void act() {
animate();
moveVertically();
horizontalMovement();
}
public void moveVertically()
{
boolean onGround = false; // assume in air
vSpeed += acceleration; // apply gravity
setLocation(getX(), getY()+vSpeed); // fall
Actor actor = getOneIntersectingObject(Collider.class);
if (actor != null)
{ // colliding with another actor
int vDir = (int)Math.signum(vSpeed); // vertical direction of movement
vSpeed = 0; // kill vertical speed
if (vDir > 0) onGround = true; // landing and not bopping head
setLocation(getX(), actor.getY()-vDir*(actor.getImage().getHeight()+getImage().getHeight())/2); // assigning stop location
}
if (onGround && Greenfoot.isKeyDown("space")) vSpeed = -20; // initiate a jump
}
public void horizontalMovement()
{
int boost = Greenfoot.isKeyDown("s") ? 9 : 0;
int dir = 0;
if (Greenfoot.isKeyDown("left")) dir--;
if (Greenfoot.isKeyDown("right")) dir++;
setLocation(getX(), getY()+6+boost*dir);
Actor actor = getOneIntersectingObject(Actor.class);
if (actor != null) setLocation(actor.getX(), getY()-dir*(actor.getImage().getWidth()+getImage().getWidth())/2);
if (dir == -1 && animSet != leftAnim) setAnim(leftAnim, 4);
else if (dir == 1 && animSet != rightAnim) setAnim(rightAnim, 4);
else if (dir == 0 && animSet != standleftAnim && animSet != standAnim) setAnim(animSet == leftAnim ? standleftAnim : standAnim, 4);
}
private void animate()
{
animCount = (animCount+1)%(animSet.length*animDelay);
if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay]);
}
}
setLocation(getX()+(6+boost)*dir, getY());
if (actor != null) setLocation(actor.getX()-dir*(actor.getImage().getWidth()+getImage().getWidth())/2, getY());