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

2012/11/24

#
I explain it easily :] I have added some more code to get out my Number from Losowanko.class
public int getNumber()
    {
    return Number;    
    }
now I dont know how to compere the "Number" to another value in different class. When I'm making something like this
if(Losowanko.getNumber() == 2)
           {}
I'm getting error.
danpost danpost

2012/11/24

#
Mateo wrote...
if(Losowanko.getNumber() == 2)
           {}
I'm getting error.
You are trying to call a method on a class. It is not the class that has the Number, but each instance of the class has a Number (if you have declared 'int Number;' in your Losowanko class). It might be best if you re-post your Losowanko class as it is right now (changes have been made to it since the last time you posted it).
Mateo Mateo

2012/11/24

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

/**
 * Write a description of class Losowanko here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Losowanko extends Actor
{
    private int Number;
    /**
     * Act - do whatever the Losowanko wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Losowanko() 
    {  
       Number = Greenfoot.getRandomNumber(4);
      
      if(Number==0)
      {
       setImage("button-blue.png");
      }
      if(Number==1)
      {
          setImage("button-green.png");
      }
      if(Number==2)
      {
          setImage("button-purple.png");
      }
      if(Number==3)
      {
          setImage("button-red.png");
      }
    }    
    public int getNumber()
    {
    return Number;    
    }
}
danpost danpost

2012/11/24

#
Alright. Now, where did you try putting the code 'if(Losowanko.getNumber()==2)'? Please show the method you had put that line in.
Mateo Mateo

2012/11/24

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

/**
 * Write a description of class pocisk here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Strzal extends Mover
{
    /**
     * Act - do whatever the pocisk wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    public void act() 
    {
      move(10.0);
      checkpileczka();
      if (getWorld() != null) checkpileczka2();  
      if (getWorld() != null) checkpileczka3();  
      if (getWorld() != null) checkpileczka4();  
      if (getWorld() != null) checkpileczka5();  
      if (getWorld() != null && getY()<=0) getWorld().removeObject(this);
    }

    public void checkpileczka()
    {
        Actor S = getOneIntersectingObject(pileczka.class);
       
        
        if (S != null)
        {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
           
           
           
        }
       
    }
    
    public void checkpileczka2()
    {
        Actor S = getOneIntersectingObject(pileczka2.class);
       
        
        if (S != null)
        {
           if(Losowanko.getNumber() == 2)
           {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
        }
           
           
        }
        
    }
    
    public void checkpileczka3()
    {
        Actor S = getOneIntersectingObject(pileczka3.class);
       
        
        if (S != null)
        {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
          
           
          
        }
        
    }
    
    public void checkpileczka4()
    {
        Actor S = getOneIntersectingObject(pileczka4.class);
       
        
        if (S != null)
        {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
            
            
       
        }
        
    }
    
    public void checkpileczka5()
    {
        Actor S = getOneIntersectingObject(pileczka5.class);
       
        
        if (S != null)
        {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
           
        }
        
    }
    
    private void ObjectHit()
    {
        Swiat swiatWorld = (Swiat) getWorld();
        Counter counter = swiatWorld.getCounter();
        counter.bumpCount(5);
    }
    
     private void ObjectMissed()
    {
        Swiat swiatWorld = (Swiat) getWorld();
        Counter counter = swiatWorld.getCounter();
        counter.bumpCount(-5);
    }
 
}
danpost danpost

2012/11/24

#
OK, change line 52 of the Strzal class to
if (((pileczka2) S).getNumber()==2)
This will let the compiler know where to start looking for the 'getNumber' method. By casting the Actor object 'S' to 'pileczka2', the compiler starts to look for the method in the 'pileczka2' class; and then, if it does not find it there, it searches for it in each successive 'super' class until it either finds it or tells you it cannot. The method has to be called on an object of the class because, otherwise, if it just returned a number from any object of that class, it would be pot luck (not neccessarily the one you wanted).
Mateo Mateo

2012/11/24

#
Error: cannot find symbol - method getNumber()
danpost danpost

2012/11/24

#
Do the 'pileczka*' classes extend the Losowanko class, or how are your classes related to each other. Like: Strzal >> Mover >> ? >> ? >> Actor ? >> ? >> Losowanko ?? Actor ? >> Swiat ?? World pileczka >> ? >> ? >> Actor pileczka2 >> ? >> ? >> Actor pileczka3 >> ? >> ? >> Actor pileczka4 >> ? >> ? >> Actor
Mateo Mateo

2012/11/24

#
pileczka/2/3/4/5 >> Mover >>Actor Losowanko >> Actor
danpost danpost

2012/11/24

#
OK, if you only have one Losowanko class object in the world you can change that same line to
if (((Losowanko) getWorld().getObjects(Losowanko.class).get(0)).getNumber()==2)
Mateo Mateo

2012/11/24

#
It works could you explain how it works?
danpost danpost

2012/11/24

#
Move line 68 to line 73 in the Strzal class (you have a misplaced close bracket '}').
Mateo Mateo

2012/11/24

#
I have corrected all mistake. Could you tell me how
if (((Losowanko) getWorld().getObjects(Losowanko.class).get(0)).getNumber()==2)  
works?
danpost danpost

2012/11/24

#
OK, the (non-static) method 'getNumber' is in the Losowanko class; so, we need a Losowanko class object to call the method on. Since you only have one Losowanko class object in the world, we can get a reference to it fairly easily. 'getWorld().' gets the World object that 'this' (Strzal object) is in. 'getObjects(Losowanko.class)' (a World method) returns a List object with all the Losowanko class objects that are in that world. 'get(0)' (a List class method) returns the first Losowanko object listed in that list as an Object object (hence the name of the method 'getObjects'). We cannot call our 'getNumber' method on an Object object (it must be a Losowanko object), so we cast the Object object with '(Losowanko)' to make it a Losowanko object. Now we can call 'getNumber' on the object and compare the return with '2'.
Mateo Mateo

2012/11/25

#
Thanks for that. Now I have some new problem ;] I'm trying to make new game started after Player don't have more lifes How I make it World Code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; 

/**
 * Write a description of class �wiat here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Swiat extends World
{

    private Counter theCounter;
    private szanse theSzanse;
    boolean NewWorld = false;
    
    
    public Swiat()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Statek statek = new Statek();
        addObject(statek, 467, 486);
        pileczka pileczka = new pileczka();
        addObject(pileczka, 328, 94);
        pileczka2 pileczka2 = new pileczka2();
        addObject(pileczka2, 712, 186);
        pileczka2.setLocation(620, 228);
        pileczka2.setLocation(649, 255);
        removeObject(pileczka2);
        removeObject(pileczka);
        pileczka2 pileczka22 = new pileczka2();
        addObject(pileczka22, 311, 105);
        pileczka pileczka3 = new pileczka();
        addObject(pileczka3, 410, 113);
        pileczka3 pileczka32 = new pileczka3();
        addObject(pileczka32, 490, 120);
        pileczka4 pileczka4 = new pileczka4();
        addObject(pileczka4, 583, 124);
        pileczka5 pileczka5 = new pileczka5();
        addObject(pileczka5, 675, 133);
        Losowanko losowanko = new Losowanko();
        addObject(losowanko, 62, 77);
        losowanko.setLocation(40, 76);
        pileczka22.setLocation(211, 52);
        pileczka3.setLocation(213, 143);
        pileczka32.setLocation(216, 236);
        pileczka4.setLocation(219, 334);
        pileczka5.setLocation(226, 429);
        pileczka2 pileczka23 = new pileczka2();
        addObject(pileczka23, 772, 51);
        pileczka pileczka6 = new pileczka();
        addObject(pileczka6, 776, 156);
        pileczka3 pileczka33 = new pileczka3();
        addObject(pileczka33, 784, 265);
        pileczka4 pileczka42 = new pileczka4();
        addObject(pileczka42, 776, 365);
        pileczka5 pileczka52 = new pileczka5();
        addObject(pileczka52, 780, 460);
        pileczka33.setLocation(769, 265);
        pileczka42.setLocation(765, 366);
        pileczka52.setLocation(765, 463);
        pileczka6.setLocation(764, 155);
        pileczka23.setLocation(761, 51);
        pileczka52.setLocation(694, 417);
        pileczka42.setLocation(450, 59);
        pileczka3.setLocation(215, 147);
        pileczka6.setLocation(758, 157);
        pileczka33.setLocation(454, 166);
        pileczka52.setLocation(756, 302);
        pileczka5.setLocation(473, 304);
        theCounter = new Counter();
        addObject(theCounter, 200, 560);
        theSzanse = new szanse();
        addObject(theSzanse, 73, 441);
        
    }
    public szanse getSzanse()
    {
        
        return theSzanse;
    }

    public Counter getCounter()
    {
        return theCounter;
    }
    
    public void YesNewWorld()
    {
     NewWorld = true;
     removeObjects(getObjects(null));
     NewWorld = false;
     prepare();
    }
   


}
Lifes code:
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
{

 public int 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();
          Lifes = 3;
          swiatWorld.YesNewWorld();
          
      }
    }    
public void Subtraction(int HowMany)
{
Lifes = Lifes - HowMany;
}
  
}
Shoot code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class pocisk here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Strzal extends Mover
{
    /**
     * Act - do whatever the pocisk wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    public void act() 
    {
      move(10.0);
      checkpileczka();
      if (getWorld() != null) checkpileczka2();  
      if (getWorld() != null) checkpileczka3();  
      if (getWorld() != null) checkpileczka4();  
      if (getWorld() != null) checkpileczka5();  
      if (getWorld() != null && getY()<=0) getWorld().removeObject(this);
    }

    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);
        }
        else
        {
        ObjectMissed();
         getWorld().removeObject(S);
         getWorld().removeObject(this);
        }
         
        }
       
    }
    
    public void checkpileczka2()
    {
        Actor S = getOneIntersectingObject(pileczka2.class);
       
        
        if (S != null)
        {
         if (((Losowanko) getWorld().getObjects(Losowanko.class).get(0)).getNumber()==1)  
           {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
        }
        else
        {
        ObjectMissed(); 
         getWorld().removeObject(S);
         getWorld().removeObject(this);
        }
         
        }
        
    }
    
    public void checkpileczka3()
    {
        Actor S = getOneIntersectingObject(pileczka3.class);
       
        
        if (S != null)
        {
         if (((Losowanko) getWorld().getObjects(Losowanko.class).get(0)).getNumber()==2)  
           {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
        }
        else
        {
         ObjectMissed(); 
         getWorld().removeObject(S);
         getWorld().removeObject(this);
        }
        
        }
        
    }
    
    public void checkpileczka4()
    {
        Actor S = getOneIntersectingObject(pileczka4.class);
       
        
       if (S != null)
        {
         if (((Losowanko) getWorld().getObjects(Losowanko.class).get(0)).getNumber()==3)  
           {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
        }
        else
        {
        ObjectMissed() ;
         getWorld().removeObject(S);
         getWorld().removeObject(this);
        }
         
        }
        
    }
    
    public void checkpileczka5()
    {
        Actor S = getOneIntersectingObject(pileczka5.class);
       
        
       if (S != null)
        {
         if (((Losowanko) getWorld().getObjects(Losowanko.class).get(0)).getNumber()==4)  
           {
           ObjectHit();
           getWorld().removeObject(S);
           getWorld().removeObject(this);
        }
        else
        {
        ObjectMissed() ;
        getWorld().removeObject(S);
        getWorld().removeObject(this);
        }
         
        }
        
    }
    
    private void ObjectHit()
    {
        Swiat swiatWorld = (Swiat) getWorld();
        Counter counter = swiatWorld.getCounter();
        counter.bumpCount(5);
    }
    
     private void ObjectMissed()
    {
        Swiat swiatWorld = (Swiat) getWorld();
        Counter counter = swiatWorld.getCounter();
        counter.bumpCount(-5);
        szanse szanse = swiatWorld.getSzanse();
        szanse.Subtraction(1);
        szanse.LifeCounter();
    }
 
}
The problem is that after making new world Im getting error like
There are more replies on the next page.
1
2
3