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

2017/1/20

Continuity problem

Hondrok Hondrok

2017/1/20

#
I want the variable armour to be avalabile for all the worlds so when the world changes the value remains the same as in the previous world (it is declared public in the actor's code)
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Goku_idle.png");
           }
        }
        else
        {
            setImage("Goku_jump.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_2());
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Final_level());
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Goku_move_right.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Goku_move_left.png");
    }
    
    public void attack()
    {
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
            setImage("Goku_attack.png");
        }
    }

    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        if(sphere != null)
        {
            armour = armour - 1;
            getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            Greenfoot.stop();
        }
    }
}
danpost danpost

2017/1/20

#
Replace line 94 with the following:
World world = new Level_2(); // reference to new world so you can work with it
Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world
mario2.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional)
Greenfoot.setWorld(world); // set new level active
Hondrok Hondrok

2017/1/21

#
danpost wrote...
Replace line 94 with the following:
World world = new Level_2(); // reference to new world so you can work with it
Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world
mario2.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional)
Greenfoot.setWorld(world); // set new level active
thank youu
You need to login to post a reply.