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

2020/11/17

HELP list size() not working properly?

Gonçalo Gonçalo

2020/11/17

#
Hi, i've tried using isEmpty(),size()==0, size()<0...basically everything and it still doesnt work. Its about 3 hearts (lifes), when they're all gone the game should end (set new world) but it never assumes there's no more lifes. However, i've coded a method for making the last heart (size()==1) blink..and it works... so it recognizes size()==1 (and ==2, ==3 ...i've tried) but never size()==0 or isEmpty(). Thanks in advance! CLASS LIFES()
public void act() 
    {         
      List lifes = getWorld().getObjects(Lifes.class);
      int life = lifes.size(); 

      if(life==1)
         {
          switchLastHeart();
         } 
      if(life==0)
        {
          VictoryNo defeat = new VictoryNo();
          Greenfoot.setWorld(defeat);
        }
    }
    
CLASS TURTLE (if is hit, 1 life's gone) protected void reduceLife() { if(isTouching(Trash.class)) { getWorld().removeObject(getWorld().getObjects(Lifes.class).get(0)); removeTouching(Trash.class); getWorld().removeObject(this); } } Objects LIFE were created in constructor world
Gonçalo Gonçalo

2020/11/17

#
protected void populateWorld()
    {
        Diver diver = new Diver();
        addObject(diver,485,400);
        
        Boat boat = new Boat();
        addObject(boat,150,265);
        
        Turtle turtle1 = new Turtle();
        addObject(turtle1,600,400);
        Turtle turtle2 = new Turtle();
        addObject(turtle2,600,470);
        Turtle turtle3 = new Turtle();
        addObject(turtle3,600,330);
        
        Fish fish1 = new Fish();
        addObject(fish1,600,370);
        Fish fish2 = new Fish();
        addObject(fish2,600,440);
        
        Lifes heart1 = new Lifes();
        Lifes heart2 = new Lifes();
        Lifes heart3 = new Lifes();

        addObject(heart1,611,21);       
        addObject(heart2,645,21);  
        addObject(heart3,679,21);   
        
        BoatHelp boathelp = new BoatHelp();
        addObject(boathelp,567,82);
        DiverHelp diverhelp = new DiverHelp();
        addObject(diverhelp,569,155);
    }     
danpost danpost

2020/11/17

#
Gonçalo wrote...
Hi, i've tried using isEmpty(),size()==0, size()<0...basically everything and it still doesnt work. Its about 3 hearts (lifes), when they're all gone the game should end (set new world) but it never assumes there's no more lifes. However, i've coded a method for making the last heart (size()==1) blink..and it works... so it recognizes size()==1 (and ==2, ==3 ...i've tried) but never size()==0 or isEmpty().
If you remove the last Lifes object from the world, there will be none to test the "life== 0" condition, because an actor only acts if it is in the world. What this boils down to is that lines 10 to 14 in Lifes need to be moved somewhere else. Game control, which this actually is, is usually best placed in the act method of your game world.
danpost danpost

2020/11/17

#
I am not sure what switchLastHeart does, but consider that the method will be executed each and every act cycle while one heart is in the world.
Gonçalo Gonçalo

2020/11/17

#
Hi, switchLastHeart basically changes the heart image between the current image and another one (empty), making the effect of blinking. Do i need to create a act method in the world then? If i add the code here it doesnt work either..
/**
     * Constructor for objects of class Game1.
     * 
     */
    public Game1()
    {    
        // Create a new world with 700x500 cells with a cell size of 1x1 pixels.
        super(700, 500, 1);  
        score1 = 0;
        score2 = 0;
        score3 = 0;
        showText("Time Left:",50,481);
        backGround1();
        populateWorld();        
        addPoints(points1,points2);          
    }
    
    protected void populateWorld()
    {
        Diver diver = new Diver();
        addObject(diver,485,400);
        
        Boat boat = new Boat();
        addObject(boat,150,265);
        
        Turtle turtle1 = new Turtle();
        addObject(turtle1,600,400);
        Turtle turtle2 = new Turtle();
        addObject(turtle2,600,470);
        Turtle turtle3 = new Turtle();
        addObject(turtle3,600,330);
        
        Fish fish1 = new Fish();
        addObject(fish1,600,370);
        Fish fish2 = new Fish();
        addObject(fish2,600,440);
        
        Lifes heart1 = new Lifes();
        Lifes heart2 = new Lifes();
        Lifes heart3 = new Lifes();

        addObject(heart1,611,21);       
        addObject(heart2,645,21);  
        addObject(heart3,679,21);  
        
        BoatHelp boathelp = new BoatHelp();
        addObject(boathelp,567,82);
        DiverHelp diverhelp = new DiverHelp();
        addObject(diverhelp,569,155);
    }     
Gonçalo Gonçalo

2020/11/17

#
It worked @danpost! I do needed to create an act method in Game1 world. Thank you so much, your the best! Best regards.
You need to login to post a reply.