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
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
in the Hero class?
No. Only in GameWorld .
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.
soumya.__.khanna soumya.__.khanna

2020/6/21

#
I haven't added the lives in the game world yet. I was going to wait until they started working in the Water World. :)
soumya.__.khanna soumya.__.khanna

2020/6/21

#
How would i make the entire hero class static doe?
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
I haven't added the lives in the game world yet. I was going to wait until they started working in the Water World. :)
That is not an issue here. It is just to initialize the lives value for the game.
soumya.__.khanna soumya.__.khanna

2020/6/21

#
O okie! Thanks for working on this
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
How would i make the entire hero class static doe?
I wrote Hero "field" -- not "class".
soumya.__.khanna soumya.__.khanna

2020/6/21

#
Field as in...?
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
How would i make the entire hero class static doe?
I wrote Hero "field" (in your Worlds class) -- not "class".
soumya.__.khanna soumya.__.khanna

2020/6/21

#
Ohhh okie, I'll try that
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
Ohhh okie, I'll try that
(line 11 in Worlds class)
soumya.__.khanna soumya.__.khanna

2020/6/21

#
Nope, didn't work either. Here is what I did
public static Hero mario = new Hero(); //Declaring the Swimming Mario Class
danpost danpost

2020/6/21

#
soumya.__.khanna wrote...
Nope, didn't work either. Here is what I did
public static Hero mario = new Hero(); //Declaring the Swimming Mario Class
Remove "= new Hero()". In child class (of Hero) constructors, add:
Worlds.mario = this;
soumya.__.khanna soumya.__.khanna

2020/6/21

#
didn't work
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 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)
        {
        }
    } 
}
danpost danpost

2020/6/21

#
And in Mario class?
There are more replies on the next page.
1
2
3
4