soumya.__.khanna wrote...
in the Hero class?the hearts are still not leaving
Still working on it. I think the Hero field will also need to be made static and each child class will need to assign its instance to it.public static Hero mario = new Hero(); //Declaring the Swimming Mario Class
public static Hero mario = new Hero(); //Declaring the Swimming Mario Class
Worlds.mario = this;
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SwimmingMario here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SwimmingMario extends Hero
{
//Arrays
private static GreenfootImage[] RIGHT =
{new GreenfootImage("SwimLeft0.gif"),
new GreenfootImage("SwimLeft1.gif"),
new GreenfootImage("SwimLeft2.gif"),
new GreenfootImage("SwimLeft3.gif"),};
private static GreenfootImage[] LEFT =
{new GreenfootImage("SwimRight0.gif"),
new GreenfootImage("SwimRight1.gif"),
new GreenfootImage("SwimRight2.gif"),
new GreenfootImage("SwimRight3.gif"),};
public SwimmingMario()
{
super();
animation = RIGHT; //Animation when staying the way it is
Worlds.mario = this;
}
public void act()
{
super.act();
marioAnimator();
whatIsGround();
jumpingJumping();
checkContact();
marioMover();
}
public void marioAnimator()
{
if(!groundLevel)
{
if (Greenfoot.isKeyDown("space") && movingLeft)
{
animateLogic(LEFT, 9);
}
else if (Greenfoot.isKeyDown("space") && movingRight)
{
animateLogic(RIGHT, 9);
}
else if (Greenfoot.isKeyDown("left") && animation != LEFT)
{
animateLogic(LEFT, 10);
}
else if (Greenfoot.isKeyDown("right") && animation != RIGHT)
{
animateLogic(RIGHT, 10);
}
}
else
{
if (Greenfoot.isKeyDown("left") && animation != GROUNDLEFT)
{
animateLogic(GROUNDLEFT, 10);
}
else if (Greenfoot.isKeyDown("right") && animation != GROUNDRIGHT)
{
animateLogic(GROUNDRIGHT, 10);
}
else if (!Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("left") && animation != IDLE)
{
animateLogic(IDLE, 50);
}
}
}
public void animateLogic(GreenfootImage[] animation, int skipRate)
{
this.animation = animation; //The animation sequence to play
this.skipRate = skipRate ; //How fast the animation will be in each case
actCounter = skipRate - 1;
frame = 0;
}
private void whatIsGround() //Check for the floor that Mario is on
{
Actor block = getOneObjectAtOffset(0, getCustomHeight(), Block.class);//Create information of platforms at offset
if(block == null)//Check for no platform
{
groundLevel = false; //is not on ground
}
else
{
//groundLevel = true; //is on ground
onFloor(block); //initiate method on Floor
gravity = 0; //No sinking
jumpingJumping(); //Allow for jumping
}
}
public void jumpingJumping()
{
if(Greenfoot.isKeyDown("space") && (!groundLevel || groundLevel)) //regular jump
{
gravity = -4;
groundLevel = false;
changeAnimation = true;
}
}
public void marioDeath()
{
getWorld().removeObject(this);
}
public void checkContact()
{
if (isTouching(Enemies.class) && damageTaken == false)
{
Enemies tempEnemy = (Enemies)getOneIntersectingObject(Enemies.class);
decreaseLives();
getWorld().removeObject(this);
//rip();
damageTaken = true;
damageTakenTimer = 50;
//Greenfoot.delay(150);
//setLocation(getWorld().getWidth()/9, getWorld().getHeight()-floatHeight);
}
if (damageTaken == true)
{
//Prevent dying continuously when colliding
damageTakenTimer--;
if(damageTakenTimer <= 0)
{
damageTaken = false;
}
}
if(this.LifeLine == 0)
{
Greenfoot.setWorld(new Death());
}
}
public void decreaseLives()
{
this.LifeLine -= 1;
}
public void marioMover()
{
try
{
if (Greenfoot.isKeyDown("left")) //Go in the left direction(accoring to speed), if left key is down
{
setLocation(getX() - speed, getY());
movingLeft = true;
}
if (Greenfoot.isKeyDown("right")) //Move right
{
setLocation(getX() + speed, getY());
movingRight = true;
}
gravity(); //Fall from the top, and after jumps
}
catch(IllegalStateException ex)
{
}
}
}