However, now neither gravity nor hit detection work and only the standing animation plays, movment animations do not play.
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 boolean jumping;
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];
static
{
for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_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");
}
private int frame = 1;
// with the following fields
private int animCount; // counter for animations
private int animDelay = 6; // initially set for standing animation (frame rate field)
// this method might help
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 int Counter = 0;
private GreenfootImage[] animSet = standAnim;
/**
* Here you can tell your actor what he has to do.
*/
public void act() {
animate();
move();
Counter();
ceiling();
}
public void Counter()
{
if(Greenfoot.isKeyDown("right"))
Counter = 0;
if(Greenfoot.isKeyDown("left"))
Counter = 1;
}
public void move()
{
int y = getY();
int x = getX();
if(Greenfoot.isKeyDown("right") ) x+=6;
if(Greenfoot.isKeyDown("left") ) x-=6;
setLocation(x,y);
}
public void fall()
{
setLocation(getX(), getY() +vSpeed);
{
vSpeed = vSpeed + acceleration;
}
jumping = true;
}
public void onGround()
{
boolean onGround = false;
// assume in air
vSpeed += acceleration; // apply gravity
setLocation(getX(), getY()+vSpeed); // fall
Actor actor = getOneIntersectingObject(Actor.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+30); // assigning stop location
}
if (onGround && Greenfoot.isKeyDown("space")) vSpeed = -20; // initiate a jump
}
public boolean ceiling()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int) (spriteHeight/-2-30);
Actor ceiling = getOneObjectAtOffset(0, yDistance, Highway.class);
if(ceiling != null)
{
vSpeed =1;
return true;
}
}
public void moveToGround(Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
setLocation(getX(), newY);
jumping = false;
}
public void jump()
{
vSpeed = vSpeed - jumpStrength;
jumping = true;
fall();
}
private void animate()
{
animCount = (animCount+1)%(animSet.length*animDelay);
if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay]);
}
}
moveVertically();
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];
static
{
for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_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");
}
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();
move();
moveVertically();
}
public void move()
{
int y = getY();
int x = getX();
if(Greenfoot.isKeyDown("right") ) x+=6;
if(Greenfoot.isKeyDown("left") ) x-=6;
setLocation(x,y);
}
public void moveVertically()
{
boolean onGround = false;
// assume in air
vSpeed += acceleration; // apply gravity
setLocation(getX(), getY()+vSpeed); // fall
Actor actor = getOneIntersectingObject(Actor.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+30); // assigning stop location
}
if (onGround && Greenfoot.isKeyDown("space")) vSpeed = -20; // initiate a jump
}
private void animate()
{
animCount = (animCount+1)%(animSet.length*animDelay);
if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay]);
}
}