This site requires JavaScript, please enable it in your browser!
Greenfoot back
soumya.__.khanna
soumya.__.khanna wrote ...

2020/6/21

Adding Lives This is very urgent so please help asap!

1
2
3
4
soumya.__.khanna soumya.__.khanna

2020/6/21

#
I added hearts in my world to represent the number of lives Mario has. Every time mario dies, a heart is subtracted from the game. But I am not sure why my heart isn't being removed when Mario is no longer alive. Here is the life code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Life here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Life extends Actor
{
    public void act() 
    {
        setLook();
    } 
    public void setLook()
    {
        GreenfootImage life = new GreenfootImage(getImage());
        setImage(life);
    } 
}

This is the World parent class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * The parent class of worlds
 * 
 * @soumya.__.khanna (Soumya Khanna) 
 * @I'llLookItUp (Jun 17th, 2020)
 */
public class Worlds extends World
{
    public int width = getWidth(); //Width of the world --Accessed in children classes
    public SwimmingMario mario = new SwimmingMario();
    //Number of hearts based on the life of Mari
    
    public Life life0;
    public Life life1;
    public Life life2;
    public Life life3;
    public Life life4;
    public Score score;
    public void act()
    {
        checkLife(mario.getLives());
    }
    public Worlds()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(2200, 576, 1, false);
    }
    public void lifeCounter(int x, int y)
    {
        life0 = new Life();
        life1 = new Life();
        life2 = new Life();
        life3 = new Life();
        life4 = new Life();
        
        
        addObject(life0, x, y);
        addObject(life1, x + 30, y);
        addObject(life2, x + 60, y);
        addObject(life3, x + 90, y);
        addObject(life4, x + 120, y);
    }
    public void checkLife(int numLives)
    {
        //Make lifeCounter represent Mario's lives 
            if(mario.getLives() == 4)
            {
                removeObject(life4);
            }
            else if(mario.getLives() == 3)
            {
                removeObject(life3);
            }
            else if(mario.getLives() == 2)
            {
                removeObject(life2);
            }
            else if(mario.getLives() == 1)
            {
                removeObject(life1);
            }
            else if(mario.getLives() == 0)
            {
                removeObject(life0);
            }
    }
    public int setLives()
    {
        return mario.getLives();
    }
    public int getLives()
    {
        return mario.LifeLine;
    }
    public Score getScore()
    {
        return score; //Information from scoreboard
    }
}
World Child class
public void act()
    {
        super.act();
    }
    public WaterWorld()
    {    
        //Placing all actors in the world
        prepare();
    }
     public void prepare()
    {
        //Adds the floor blocks
        addFloor();
        //First row of blocks
        topBlocks(getWidth() - 2248 ,getHeight()- 500); //Left chunck of blocks
        topBlocks(getWidth() - 200,getHeight()- 500); //Right blocks
        middleBlocks(getWidth()/2 ,getHeight()- 500); // Main chunk of blocks in the middle of the world
        //Smaller chuncks of blocks on each side
        sideBlocks(getWidth()/3 - 500 ,getHeight()- 310); //Left Side
        sideBlocks(getWidth()/3 + 1300 ,getHeight()- 310); //Right Side
        //Main hero
        addObject(new SwimmingMario(), getWidth()/8, getHeight()/2);
        //Adding Cheep Cheeps
        addEnimies();
        //Placing the hearts that represent Mario's life
        lifeCounter(getWidth()/30, getHeight() - 50);
        lifeCounter(getWidth() - 150, getHeight() - 50);
    }
And thats mario
private int frame;
    private int actCounter;
    private int skipRate;
    private int floor = 448;
    //doubles to allow decimals
    private double gravity, gravityIncrement;
    //Booleans
    private boolean groundLevel;
    private boolean changeAnimation;
    private boolean movingRight;
    private boolean movingLeft; 
    private boolean damageTaken = false; //Hero has not taken any damage
    private int damageTakenTimer = 0; 
    public SwimmingMario() 
    {
        this.LifeLine = 5;
        frame = 0; //initial frame
        skipRate = 75; //speed of animaation
        actCounter = 0; //initial act
        animation = RIGHT; //Animation when staying the way it is 
        speed = 3; //How fast it moves
        gravity = 0; //Don't sink
        gravityIncrement = .1; //Velocity 
    }
    public void act() 
    {
        attack();
        triggerReleased = true; //Trigger is released
        updateAmimation();
        actCounter++;
        marioAnimator();
        //blockMovement();
        whatIsGround();
        marioMover();
        floatingMario();
        jumpingJumping();
        checkCeiling();
        checkContact();
    }
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;
            }
        }  
    }
    public void decreaseLives()
    {
        this.LifeLine -= 1;
    }
public int getLives()
    {
        return this.LifeLine;
    }
danpost danpost

2020/6/21

#
You must have a Mario parent class also as I do not see where LifeLine is declared.
soumya.__.khanna soumya.__.khanna

2020/6/21

#
Yes! There is a mario parent class. I organized things a bit more. Here you go Mario Parent:
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 Actor
{
    
    public static GreenfootImage[]IDLE = 
                                            {new GreenfootImage("Idle0.png"),
                                             new GreenfootImage("Idle1.png"),
                                             new GreenfootImage("Idle2.png"),
                                             new GreenfootImage("Idle3.png"),
                                             new GreenfootImage("Idle4.png"),
                                             new GreenfootImage("Idle5.png"),
                                             new GreenfootImage("Idle6.png"),
                                             new GreenfootImage("Idle7.png")};
    
    public static GreenfootImage[] GROUNDLEFT =
                                            {new GreenfootImage("Left1.png"),
                                             new GreenfootImage("Left2.png"),
                                             new GreenfootImage("Left3.png")};
    public static GreenfootImage[] GROUNDRIGHT =
                                            {new GreenfootImage("Right1.png"),
                                             new GreenfootImage("Right2.png"),
                                             new GreenfootImage("Right3.png")};
    
    
    public boolean triggerReleased; //Once weapon has been released
    public int attackCycle; //For the hero to attack
    public int LifeLine;
    public int speed; //Refers to the heros' speed, and health
    
    public int frame;
    public int actCounter;
    public int skipRate;
    public int floor = 448;
    
    public GreenfootImage[] animation;
    //ints
    //doubles to allow decimals
    public double gravity, gravityIncrement;
    //Booleans
    public boolean groundLevel;
    public boolean changeAnimation;
    public boolean movingRight;
    public boolean movingLeft; 
    public boolean damageTaken = false; //Hero has not taken any damage
    public int damageTakenTimer = 0; 
    public void act() 
    {
        updateAmimation();
        actCounter++;
        attack();
        triggerReleased = true; //Trigger is released
        marioMover();
        checkCeiling();
        floatingMario();
    } 
    public Hero()
    {
        this.LifeLine = 5;
        frame = 0; //initial frame
        skipRate = 75; //speed of animaation
        actCounter = 0; //initial act
        
        speed = 3; //How fast it moves
        gravity = 0; //Don't sink
        gravityIncrement = .1; //Velocity 
    }
    public void updateAmimation()
    {
        if(animate())
        {
            setImage(animation[frame]);
            frame ++;
            if( frame >= animation.length)
            {
                frame = 0;
            }
        }
    }
    public boolean animate()
    {
        return actCounter % skipRate == 0;
    }    
    public void fireWeapons(FireBall fireThis)
    {
        fireThis.setRotation (getRotation());
        getWorld().addObject (fireThis,getX(),getY());
        fireThis.move(getImage().getWidth()/2);
    }
    public void attack()
    {
        if(attackCycle <= 0) //To avoid rapid shooting
        {
            if(Greenfoot.isKeyDown("M"))
            {
                fireWeapons(new FireBall(3, 3)); //Shoot the weapons depending on whether or not key is pressed
            }
            else
            {
                triggerReleased = true; //Otherwise, trigger is released.
            }
            attackCycle = 10; // n being the cooldown in act cycles.    
        }
        else if(attackCycle > 0)
        {
            attackCycle--; // decrement by 1 per act cycle.
        }        
    }    
    public void marioMover()
    {
        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
    }    
    public void gravity()
    {
        setLocation(getX(), getY() +(int) gravity); //Stay at the same x value, but fall with gravity
        gravity = gravity + gravityIncrement; //Adding velocity
    }    
    public void onFloor(Actor block)
    {
        int blockHeight = block.getImage().getHeight(); //Height of the block
        int blockTop = block.getY() - blockHeight/2; //Height of a part of the block
        setLocation(getX(), blockTop-getCustomHeight()); //Change to this location
        skipRate = skipRate; //Reset jumping animation to normal speed
        groundLevel = true;
    }   
    private void checkCeiling() 
    {
        try
        {
            Actor ceiling = getOneObjectAtOffset(0, -getCustomHeight(), Block.class);//Create information of platforms at offset
            if(ceiling != null)//Check for platform(double negative)
            {
                groundLevel = false;
                goToCeiling(ceiling);
            }
        }
        catch(IllegalStateException ex)
        {
            //yay
        }
    }
    private void goToCeiling(Actor ceiling) //Check for what is on top of Mario's head
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int ceilingBottom = ceiling.getY() + ceilingHeight/2;
        setLocation(getX(), ceilingBottom+getCustomHeight()); //Set this location
    }   
    public void floatingMario()
    {
        if (getY() >= floor) //Don't go past the floor
        {
            setLocation(getX(), floor);
            //marioDeath();
        }
    }
    //Getters and Setters    
    public int getSpeed()
    {
        return speed; //Get information from speed
    }
    public void setSpeed(int s)
    {
        speed = s; 
    }
    public int getHeight() 
    {
        return getImage().getHeight();
    }    
    public int getCustomHeight() 
    {
        return getImage().getHeight()/4;
    }
    public int getWidth()
    {
        return getImage().getWidth();
    }    
    public int getCustomWidth()
    {
        return getImage().getWidth()/4;
    }  
    public int getLifeLine()
    {
        return LifeLine;
    }
}
Swimming Mario:
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 
    }
    public void act() 
    {
        super.act();
        marioAnimator();
        whatIsGround();
        jumpingJumping();
        checkContact();
    }
    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;
            }
        }  
    }
    public void decreaseLives()
    {
        this.LifeLine -= 1;
    }
    public int getLives()
    {
        return this.LifeLine;
    }
}
soumya.__.khanna soumya.__.khanna

2020/6/21

#
Sorry about that!
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
Sorry about that!
Well, the problem wasn't there after all. Change new SwimmingMario() in line 22 of WaterWorld class (above) to mario.
soumya.__.khanna soumya.__.khanna

2020/6/21

#
But in my world Class, I declared SwimmingMario as mario, so would that make a difference? Because I already have a regular Mario class
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
But in my world Class, I declared SwimmingMario as mario, so would that make a difference? Because I already have a regular Mario class
Oh ... two different types of "mario" heroes. Yes, then .. a big difference. Each Hero instance, is given its own LifeLine field (even if two were created from the same child class).
soumya.__.khanna soumya.__.khanna

2020/6/21

#
I tired that as well, didn't work. :((
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
I tired that as well, didn't work. :((
I do not think you understood my point. Let's say you create a Mario object. It starts with 5 lives. Okay, say now it loses 2 lives and has only 3 remaining. Next, you create a SwimmingMario object. It will also start with 5 lives. It matters not how many lives the Mario object has left. So, I am saying that this is why you are having issues. Using LifeLine in the Hero class, as you have it -- as an instance field, is not going to work as you had intended. My suggestion is to make LifeLine a static field and make sure you set it to zero in your initial world constructor. The getLives method can also be made static. These should only be in your Hero class.
soumya.__.khanna soumya.__.khanna

2020/6/21

#
Okie, I'll try that out and let you know
soumya.__.khanna soumya.__.khanna

2020/6/21

#
Now the game isn't playing. It just goes onto the death screen
soumya.__.khanna soumya.__.khanna

2020/6/21

#
This is what I did
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 Actor
{
    
    public static GreenfootImage[]IDLE = 
                                            {new GreenfootImage("Idle0.png"),
                                             new GreenfootImage("Idle1.png"),
                                             new GreenfootImage("Idle2.png"),
                                             new GreenfootImage("Idle3.png"),
                                             new GreenfootImage("Idle4.png"),
                                             new GreenfootImage("Idle5.png"),
                                             new GreenfootImage("Idle6.png"),
                                             new GreenfootImage("Idle7.png")};
    
    public static GreenfootImage[] GROUNDLEFT =
                                            {new GreenfootImage("Left1.png"),
                                             new GreenfootImage("Left2.png"),
                                             new GreenfootImage("Left3.png")};
    public static GreenfootImage[] GROUNDRIGHT =
                                            {new GreenfootImage("Right1.png"),
                                             new GreenfootImage("Right2.png"),
                                             new GreenfootImage("Right3.png")};
    
    
    public boolean triggerReleased; //Once weapon has been released
    public int attackCycle; //For the hero to attack
    public static int LifeLine;
    public int speed; //Refers to the heros' speed, and health
    
    public int frame;
    public int actCounter;
    public int skipRate;
    public int floor = 448;
    
    public GreenfootImage[] animation;
    //ints
    //doubles to allow decimals
    public double gravity, gravityIncrement;
    //Booleans
    public boolean groundLevel;
    public boolean changeAnimation;
    public boolean movingRight;
    public boolean movingLeft; 
    public boolean damageTaken = false; //Hero has not taken any damage
    public int damageTakenTimer = 0; 
    public void act() 
    {
        updateAmimation();
        actCounter++;
        attack();
        triggerReleased = true; //Trigger is released
        marioMover();
        checkCeiling();
        floatingMario();
    } 
    public Hero()
    {
        this.LifeLine = 0;
        frame = 0; //initial frame
        skipRate = 75; //speed of animaation
        actCounter = 0; //initial act
        
        speed = 3; //How fast it moves
        gravity = 0; //Don't sink
        gravityIncrement = .1; //Velocity 
    }
    public void updateAmimation()
    {
        if(animate())
        {
            setImage(animation[frame]);
            frame ++;
            if( frame >= animation.length)
            {
                frame = 0;
            }
        }
    }
    public boolean animate()
    {
        return actCounter % skipRate == 0;
    }    
    public void fireWeapons(FireBall fireThis)
    {
        fireThis.setRotation (getRotation());
        getWorld().addObject (fireThis,getX(),getY());
        fireThis.move(getImage().getWidth()/2);
    }
    public void attack()
    {
        if(attackCycle <= 0) //To avoid rapid shooting
        {
            if(Greenfoot.isKeyDown("M"))
            {
                fireWeapons(new FireBall(3, 3)); //Shoot the weapons depending on whether or not key is pressed
            }
            else
            {
                triggerReleased = true; //Otherwise, trigger is released.
            }
            attackCycle = 10; // n being the cooldown in act cycles.    
        }
        else if(attackCycle > 0)
        {
            attackCycle--; // decrement by 1 per act cycle.
        }        
    }    
    public void marioMover()
    {
        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
    }    
    public void gravity()
    {
        setLocation(getX(), getY() +(int) gravity); //Stay at the same x value, but fall with gravity
        gravity = gravity + gravityIncrement; //Adding velocity
    }    
    public void onFloor(Actor block)
    {
        int blockHeight = block.getImage().getHeight(); //Height of the block
        int blockTop = block.getY() - blockHeight/2; //Height of a part of the block
        setLocation(getX(), blockTop-getCustomHeight()); //Change to this location
        skipRate = skipRate; //Reset jumping animation to normal speed
        groundLevel = true;
    }   
    private void checkCeiling() 
    {
        try
        {
            Actor ceiling = getOneObjectAtOffset(0, -getCustomHeight(), Block.class);//Create information of platforms at offset
            if(ceiling != null)//Check for platform(double negative)
            {
                groundLevel = false;
                goToCeiling(ceiling);
            }
        }
        catch(IllegalStateException ex)
        {
            //yay
        }
    }
    private void goToCeiling(Actor ceiling) //Check for what is on top of Mario's head
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int ceilingBottom = ceiling.getY() + ceilingHeight/2;
        setLocation(getX(), ceilingBottom+getCustomHeight()); //Set this location
    }   
    public void floatingMario()
    {
        if (getY() >= floor) //Don't go past the floor
        {
            setLocation(getX(), floor);
            //marioDeath();
        }
    }
    //Getters and Setters    
    public int getSpeed()
    {
        return speed; //Get information from speed
    }
    public void setSpeed(int s)
    {
        speed = s; 
    }
    public int getHeight() 
    {
        return getImage().getHeight();
    }    
    public int getCustomHeight() 
    {
        return getImage().getHeight()/4;
    }
    public int getWidth()
    {
        return getImage().getWidth();
    }    
    public int getCustomWidth()
    {
        return getImage().getWidth()/4;
    }  
    public static int getLifeLine()
    {
        return LifeLine;
    }
}
danpost danpost

2020/6/21

#
danpost wrote...
make sure you set it to zero in your initial world constructor
I meant -- set it to 5.
soumya.__.khanna soumya.__.khanna

2020/6/21

#
It's still doing the same thing
soumya.__.khanna soumya.__.khanna

2020/6/21

#
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 int LifeLine;
    public SwimmingMario() 
    {
        super();
        animation = RIGHT; //Animation when staying the way it is 
    }
    public void act() 
    {
        super.act();
        marioAnimator();
        whatIsGround();
        jumpingJumping();
        checkContact();
    }
    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;
    }
}
There are more replies on the next page.
1
2
3
4