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

2018/4/29

Lose life

LobsterL LobsterL

2018/4/29

#
my lives aren't getting removed from the world and i don't know why. Can someone please help.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    // int getX() x = Car3X;
    int fastcarTimer = 100;
    int slowcarTimer = 100;
    Lives life1 = new Lives();
    Lives life2 = new Lives();
    Lives life3 = new Lives();
    Frog frog = new Frog();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 900, 1); 
        addObject(new Finish(), 305, 32); // adding the finish lilypad
        addObject(new Finish(), 515, 32); // adding the finish lilypad
        addObject(new Finish(), 713, 32); // adding the finish lilypad
        addObject(new Finish(), 905, 32); // adding the finish lilypad
        addObject(new Finish(), 95, 32); // adding the finish lilypad
        addObject(new Car1(), 10, 630);
        addObject(new Car1(), 500, 630);
        addObject(new Car1(), 900, 630);
        addObject(new Car1(), 300, 630);
        addObject(new Car3(), 995, 537);
        addObject(new Car3(), 750, 537);
        addObject(new Car3(), 395, 537);
        addObject(new Car3(), 195, 537);
        addObject(new Car3(), 95, 537);
        addObject(new Car4(), 195, 736);
        addObject(new Car2(), 300, 736);
        addObject(new Car4(), 600, 736);
        addObject(new Car2(), 994, 736);
        addObject(new Car2(), 800, 736);
        addObject(new Frog(), 500, 850);
        showText("Lives : " , 40, 880);
    }
    public void act()
    {
        // if (Car3X == 0)
        // {
        // }
        randomslowCarSpawner();
        randomfastCarSpawner();
        createLives();
    }
    public void slowCarSpawn()
    {
    }
    public void fastCarSpawn()
    {
    }
    public void createLives()
    {
        if(frog.getLives() == 3)
        {
          addObject(life1, 90, 880);
          addObject(life2, 120, 880);  
          addObject(life3, 150, 880);  
        }else if(frog.getLives() == 2)
        {
            addObject(life1, 90, 880);
            addObject(life2, 120, 880);  
        }else if(frog.getLives() == 1)
        {
            addObject(life1, 90, 880);
        }
            // public void removeAtWorldEdge()
        // {
        // if (isAtEdge(Car3()))
        // {
            // removeObject(Car3());
            // spawnTimer();
            
        // }
        // }
        // public void spawnTimer()
       // {
        // spawnTimer++;
        // if (spawnTimer == 0)
        // {
            // addObject(new Car3(), 36, 36);
        // }
        // } 
    }
        public void randomfastCarSpawner()
    {
        fastcarTimer++;
        if(Greenfoot.getRandomNumber(60)<50&&fastcarTimer == 100)
        {
            addObject(new Car1(),10, 630);
        }
        if(Greenfoot.getRandomNumber(50)<40&&fastcarTimer == 100)
        {
            addObject(new Car3(),995, 537);
        }
        if(fastcarTimer == 101)
        {
            fastcarTimer = 0;
        }
    }
    public void randomslowCarSpawner()
    {
        slowcarTimer++;
        if(Greenfoot.getRandomNumber(50)<70&&slowcarTimer == 100)
        {
            addObject(new Car4(),995, 736);
        }
        if(Greenfoot.getRandomNumber(40)<60&&slowcarTimer == 100)
        {
            addObject(new Car2(),995, 736);
        } 
        if(slowcarTimer == 101)
        {
            slowcarTimer = 0;
        }
    }
    public void reset()
    {
        if(frog.getLives() == 0)
        {
            Greenfoot.stop();
        }
        else if(frog.getLives() == 2){
            removeObject(life1);
        }else{
           removeObject(life2);
        }
        frog.setLocation(800,850);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Frog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Frog extends Actor
{
    private String[] move = {"frogup1.png","frogup2.png","frogup3.png","frogup4.png","frogup5.png","frogup6.png"};
    private int frameCounterMove = 0;
    private int reload;
    int lives = 3;
    Lives life = new Lives();
    /**
     * Act - do whatever the Frog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        removeWhenTouching();
    }  
    public void move()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
            setRotation(360);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-5, getY());
            setRotation(270);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+5, getY());
            setRotation(90);
            animateOnMove();
        }
        
        else if (Greenfoot.isKeyDown("down"))
        {   
            setLocation(getX(), getY()+5);
            setRotation(180);
            animateOnMove();
        }
    }
    public void animateOnMove()
    {
        reload++;
        if(reload==3)
       {
        setImage(move[frameCounterMove]);
        frameCounterMove++;
        if(frameCounterMove >= move.length)
          frameCounterMove=0; 
        reload = 0;
      }
    }
    public int getLives()
    {
       return lives;
    }
    public void loseLife()
    {
         lives = lives - 1;
    }
    public void removeWhenTouching()
    {
                if(isTouching(Car1.class))
        {
            if(getLives() == 0)
            {
                
            }else{
                loseLife();
            }
            reset();
            setLocation(800, 850);
        }
               if(isTouching(Car2.class))
        {
            if(getLives() == 0)
            {
                
            }else{
                loseLife();
            }
            reset();
            setLocation(500, 850);
        }
               if(isTouching(Car3.class))
        {
            if(getLives() == 0)
            {
                
            }else{
                loseLife();
            }
            reset();
            setLocation(500, 850);
        }
               if(isTouching(Car4.class))
        {
            if(getLives() == 0)
            {
                
            }else{
                loseLife();
            }
            reset();
            setLocation(500, 850);
        }
    }
    public void reset()
    {
        if(this.getWorld() instanceof MyWorld)
        {
            ((MyWorld) this.getWorld()).reset();
        }
    }
    }
danpost danpost

2018/4/29

#
LobsterL wrote...
my lives aren't getting removed from the world
In the MyWorld class, the frog in the frog field is not the same frog as that which is added into the world.
LobsterL LobsterL

2018/4/29

#
i have changed the code but now the frog which is initially in the world isn't removed
danpost danpost

2018/4/29

#
LobsterL wrote...
i have changed the code but now the frog which is initially in the world isn't removed
Show what you did.
LobsterL LobsterL

2018/4/29

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    // int getX() x = Car3X;
    int fastcarTimer = 100;
    int slowcarTimer = 100;
    int logSpawner = 100;
    Lives life1 = new Lives();
    Lives life2 = new Lives();
    Lives life3 = new Lives();
    Frog frog = new Frog();
    Log1 log1 = new Log1();
    Log2 log2 = new Log2();
    Log3 log3 = new Log3(); 
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 900, 1); 
        addObject(new Finish(), 305, 32); // adding the finish lilypad
        addObject(new Finish(), 515, 32); // adding the finish lilypad
        addObject(new Finish(), 713, 32); // adding the finish lilypad
        addObject(new Finish(), 905, 32); // adding the finish lilypad
        addObject(new Finish(), 95, 32); // adding the finish lilypad
        addObject(new Car1(), 10, 630);
        addObject(new Car1(), 500, 630);
        addObject(new Car1(), 900, 630);
        addObject(new Car1(), 300, 630);
        addObject(new Car3(), 995, 537);
        addObject(new Car3(), 750, 537);
        addObject(new Car3(), 395, 537);
        addObject(new Car3(), 195, 537);
        addObject(new Car3(), 95, 537);
        addObject(new Car2(), 300, 736);
        addObject(new Car2(), 994, 736);
        addObject(new Car2(), 800, 736);
        addObject(new Frog(), 500, 850);
        showText("Lives : " , 40, 880);
        addObject(new Log1(), 183, 366);
        addObject(new Log2(), 537, 366);
        addObject(new Log2(), 846, 366);
        addObject(new Log3(), 107, 282);
        addObject(new Log1(), 408, 282);
        addObject(new Log2(), 778, 282);
        addObject(new Log3(), 214, 204);
        addObject(new Log2(), 475, 204);
        addObject(new Log1(), 811, 204);
        addObject(new Log3(), 106, 136);
        addObject(new Log2(), 395, 136);
        addObject(new Log1(), 774, 136);
        addObject(new Boundary(), 1000, 00);
    }
    public void act()
    {
        // if (Car3X == 0)
        // {
        // }
        randomslowCarSpawner();
        randomfastCarSpawner();
        createLives();
        logSpawner();
    }
    public void slowCarSpawn()
    {
    }
    public void fastCarSpawn()
    {
    }
    public void createLives()
    {
        if(frog.getLives() == 3)
        {
          addObject(life1, 90, 880);
          addObject(life2, 120, 880);  
          addObject(life3, 150, 880);  
        }else if(frog.getLives() == 2)
        {
            addObject(life1, 90, 880);
            addObject(life2, 120, 880);  
        }else if(frog.getLives() == 1)
        {
            addObject(life1, 90, 880);
        }
            // public void removeAtWorldEdge()
        // {
        // if (isAtEdge(Car3()))
        // {
            // removeObject(Car3());
            // spawnTimer();
            
        // }
        // }
        // public void spawnTimer()
       // {
        // spawnTimer++;
        // if (spawnTimer == 0)
        // {
            // addObject(new Car3(), 36, 36);
        // }
        // } 
    }
        public void randomfastCarSpawner()
    {
        fastcarTimer++;
        if(Greenfoot.getRandomNumber(40)<50&&fastcarTimer == 100)
        {
            addObject(new Car1(),10, 630);
        }
        if(Greenfoot.getRandomNumber(30)<40&&fastcarTimer == 100)
        {
            addObject(new Car3(),995, 537);
        }
        if(fastcarTimer == 101)
        {
            fastcarTimer = 0;
        }
    }
    public void randomslowCarSpawner()
    {
        slowcarTimer++;
        if(Greenfoot.getRandomNumber(40)<60&&slowcarTimer == 100)
        {
            addObject(new Car2(),995, 736);
        } 
        if(slowcarTimer == 101)
        {
            slowcarTimer = 0;
        }
    }
    public void logSpawner()
    {
       logSpawner++;
        if(Greenfoot.getRandomNumber(100)<80&&logSpawner == 100)
       {
         addObject(new Log1(), 30, 366);
       }
        if(Greenfoot.getRandomNumber(100)<80&&logSpawner == 100)
       {
         addObject(new Log2(), 30, 282);
       }
        if(Greenfoot.getRandomNumber(100)<80&&logSpawner == 100)
       {
         addObject(new Log3(), 30, 204);
       }
       if(Greenfoot.getRandomNumber(100)<80&&logSpawner == 100)
       {
         addObject(new Log2(), 30, 136);
       }
       if(logSpawner == 101)
       {
         logSpawner = 0;
       }
    }
    public void addedToWorld()
    {
      
    }
    public void reset()
    {
        if(frog.getLives() == 0)
        {
            Greenfoot.stop();
        }
        else if(frog.getLives() == 2){
           removeObject(life1);
        }else{
           removeObject(life2);
        }
        removeObject(frog);
        addObject(frog, 500, 850);
    }
}
danpost danpost

2018/4/29

#
You are still creating two Frog object -- one in line 18 and another in line 47.
LobsterL LobsterL

2018/4/29

#
When i removed line 18 nothing was working
LobsterL LobsterL

2018/4/29

#
when i remove line 47 and call the reset method in the act method the frog doesn't move
LobsterL LobsterL

2018/4/29

#
Don't worry it is working fine now . I just added a start method instead of putting the frog in the myWorld
danpost danpost

2018/4/29

#
Line 18 did not add one into the world -- that is what line 47 does. The problem is that the Frog object which is created on line 18 is not the Frog object that line 47 adds into the world.
You need to login to post a reply.