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

2012/11/16

How to go out of loop when object is deleted

1
2
3
danpost danpost

2012/11/25

#
The easiest way is probably to remove your boolean 'NewWorld' altogether and change your YesNewWorld method to:
public void YesNewWorld()
{
    Greenfoot.setWorld(new Swiat());
}
Mateo Mateo

2012/11/25

#
Ok its better, but for example when I want to get new world but dont change my score and lifes value ?
danpost danpost

2012/11/25

#
You may not be using the same accessing technique(s) as I have depicted in the following, but the idea of how to accomplish what you want to do should be apparent:
public void YesNewWorld()
{
    Swiat world = new Swiat();
    world.getCounter().setValue(getCounter().getValue());
    world.getLives().setValue(getLives().getValue());
    Greenfoot.setWorld(world);
}
Mateo Mateo

2012/11/25

#
I made like that:
Swiat world = new Swiat();  
    int a = getCounter().getValue();
    world.getCounter().setValue(a);
    int b = getLives().getValue();
    world.getLives().setValue(b); 
    Greenfoot.setWorld(world); 
and
public void setValue(int a){

totalCount = a;
}
But now when a newe World began the Counter is 0, it updates only I shoot a ball Counter code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
  private int totalCount;

  
    public Counter()
    {
        setImage(new GreenfootImage("SCORE:  " +totalCount , 30, Color.BLACK, Color.WHITE));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("SCORE:  " + totalCount, 30, Color.BLACK, Color.WHITE));
    }


public int getValue(){
return totalCount;
}  
public void setValue(int a){

totalCount = a;
}

}
danpost danpost

2012/11/25

#
Chang your 'setValue' method in the Counter class to
public void setValue(int a) {
    totalCount = a;
    bumpCount(0);
}
Mateo Mateo

2012/11/25

#
I dont have solution for my problem and I get next one I made 2 kinds of adding new worlds 1st when you dont have lifes
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class szanse here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class szanse extends Actor
{
boolean NewGameStarted;
 
 public int Lifes;
 public szanse()
 {
 if(NewGameStarted = true)
 {
     Lifes = 3;
    }
 }
 public void LifeCounter() 
    {
    if(Lifes==3)
      {
       setImage("3szanse (Custom).jpg");
      }
      if(Lifes==2)
      {
          setImage("2szanse (Custom).jpg");
      }
      if(Lifes==1)
      {
          setImage("1szanse (Custom).jpg");
      }
      if(Lifes==0)
      {
          Swiat swiatWorld = (Swiat) getWorld();
          NewGameStarted = true;
          swiatWorld.YesNewWorld();
          
          
      }
    }   
public int getValue(){
return Lifes;
}    
    
public void Subtraction(int HowMany)
{
Lifes = Lifes - HowMany;
}
  public void setValue(int b){
Lifes = b;
NewGameStarted = false ; 


}}
2nd when you shoot all of objects that are given in Losowanko.class (but I have made it only for one yet)
public void checkpileczka()
    {
        Actor S = getOneIntersectingObject(pileczka.class);
       
        
        if (S != null)
        {
         if (((Losowanko) getWorld().getObjects(Losowanko.class).get(0)).getNumber()==0)  
           {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
           
           if(getWorld().getObjects(pileczka.class).size() == 0)
           {
            Swiat swiatWorld = (Swiat) getWorld();
            swiatWorld.Done();
            }
            else
            {}
           
        }
        else
        {
        ObjectMissed();
         getWorld().removeObject(S);
         getWorld().removeObject(this);
        }
         
        }
       
    }
It dont work getting NullPointerExpectation error in Strzal.checkpileczka(36)
danpost danpost

2012/11/25

#
You do not have to call 'YesNewWorld' every time. In the 'LifeCounter' method, change the last 'if' block to
if (Lifes == 0) {
    Greenfoot.setWorld(new Swiat());
}
And you probably do not need the boolean 'NewGameStarted'. Also, your szanse class object should update its image anytime the value of Lifes changes (something like we just did with the Counter class).
Mateo Mateo

2012/11/25

#
Corrected How you think about this code:
if(getWorld().getObjects(pileczka.class).size() == 0)
           {
            Swiat swiatWorld = (Swiat) getWorld();
            swiatWorld.Done();
            }
            else
            {}
I check size of list if its 0 (no objects this class in world) I make new world with saved score and lifes. But as I said there is error NullPointerExpectation
danpost danpost

2012/11/25

#
Move line 12 in your 'checkpileczka' method to line 21. 'getWorld' at line 14 will be 'null' after you execute line 12 as it is above.
Mateo Mateo

2012/11/25

#
It works thanks a lot
You need to login to post a reply.
1
2
3