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/16

#
Hello! I want to get out of loop when I delete object. Now I get error cuz Im doing something on not existing object. Help
erdelf erdelf

2012/11/16

#
post the loop, then we can help you
Mateo Mateo

2012/11/16

#
public void act() { move(10.0); checkpileczka(); checkpileczka2(); checkpileczka3(); checkpileczka4(); checkpileczka5(); if(getY()<=0) { getWorld().removeObject(this); } } but in public void checkpileczka() { Actor S = getOneIntersectingObject(pileczka.class); if (S != null) { getWorld().removeObject(S); getWorld().removeObject(this); } } so when I delete object I dont want to do for example checkpileczka2() but wait until the other instuction do new one
Mateo Mateo

2012/11/16

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. I get this info
erdelf erdelf

2012/11/16

#
well, the act cycle shouldnt be called again after the object is dead
Mateo Mateo

2012/11/16

#
What should I do for it?
Mateo Mateo

2012/11/16

#
I made it like this:
public void act() 
    {
       move(10.0);
       if (getWorld() == null) return; 
       checkpileczka();
       if (getWorld() == null) return; 
       checkpileczka2();
       if (getWorld() == null) return; 
       checkpileczka3();
       if (getWorld() == null) return; 
       checkpileczka4();
       if (getWorld() == null) return; 
       checkpileczka5();
       if (getWorld() == null) return; 
    
       if(getY()<=0)
       {
           getWorld().removeObject(this);
   
    }    
There is some better method ?
darkmist255 darkmist255

2012/11/17

#
Mateo, what I do is I make a boolean called "remove", then at the end of the act cycle do
if(remove) getWorld().removeObject(this);
That way it will only remove it at the very end of every single thing it wants to do. It avoids the error you're getting. You would just replace "removeObject(this)" with "remove = true"
danpost danpost

2012/11/17

#
darkmist255 wrote...
Mateo, what I do is I make a boolean called "remove", then at the end of the act cycle do
if(remove) getWorld().removeObject(this);
That way it will only remove it at the very end of every single thing it wants to do. It avoids the error you're getting. You would just replace "removeObject(this)" with "remove = true"
Even with this, Mateo would have to say
if (!remove) checkpileczka2();
if (!remove) checkpileczka3();
// etc..
Better, would be
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);
}
If all these method calls that begin with 'checkpile' are checks for intersecting objects of similar type, then if they were all sub-classes of the same super-class (that was a sub-class of Actor), you could perform just one check for any intersecting objects of the super-class (which would include any sub-classes).
darkmist255 darkmist255

2012/11/17

#
@danpost
    if(remove) getWorld().removeObject(this);  
This works fine and you only have to use it once, the only catch is it has to be the last step executed in the act() cycle. If anything earlier in the cycle tripped it off, it will wait for the cycle to finish before removing to avoid error. Your suggestion of course works as well, but depending on how finicky the Class is the remove boolean method might just be a simple shortcut.
danpost danpost

2012/11/17

#
I was not saying that the code would not execute. What I meant was that if only one intersecting object is to be removed, then we will need the checks in there anyway. If it does not matter that possibly two or three 'checkpileczka*' objects are removed at one time, then what you suggested would be fine.
Mateo Mateo

2012/11/17

#
Thx for help.
Mateo Mateo

2012/11/17

#
I have one more question. In program I have many (5) different of color balls. I want to do program with get random color of the ball show the color in right corner of program. User have to shoot correct ball (if he hit the ball it will be removed) else nothing heppens. What I have up till now 1. Working shoot system. 2. Working objects (ball) movment. 3. Working space movment. What I dont know how to do: 1. How choose a ball to shoot. Any help ?
danpost danpost

2012/11/17

#
The Ball class should have a constructor that randomly chooses its color and saves the number of the color it uses. Or, you can pass the number of the color into the constructor by way of a parameter.
// with instance variable of
int colorValue;
// constructor start for random coloring
public Ball()
{
    colorValue = Greenfoot.getRandomNumber(5);
// alternate constructor start for fixed coloring
public Ball(int colorVal)
{
    colorValue = colorVal;
// continuation of constructor for either fixed or random coloring
    Color color = "";
    if (colorValue == 0) color = Color.yellow;
    if (colorValue == 1) color = Color.blue;
    if (colorValue == 2) color = Color.red;
    if (colorValue == 3) color = Color.orange;
    if (colorValue == 4) color = Color.green;
    GreenfootImage image = new GreenfootImage(size, size);
    image.setColor(color);
    image.fillOval(0, 0, size - 1, size - 1);
}
// add the following method to return 'colorNumber' value
public int getColorValue()
{
    return colorValue;
}
Then in your Shot class, you should have similar constructor and variable and method (with slight modifications); and you can compare intersecting balls with the target color with (as you did not give me a class name for the object in the corner that shows the target color, I am calling it 'TargetColorBox')
Ball ball = (Ball) getOneIntersectingObject(Ball.class);
if (ball == null) return;
int ballColorValue = ball.getColorValue();
int targetColorValue = ((TargetColorBox) getWorld().getObjects(TargetColorBox.class).get(0)).getColorValue();
if (ballColorValue == targetColorValue)  getWorld().removeObject(ball);
Mateo Mateo

2012/11/24

#
I made some new class that get random number and set image (color of the ball)
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
{
    /**
     * 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() 
    {  
      int Numer = Greenfoot.getRandomNumber(4);
      
      if(Numer==0)
      {
       setImage("button-blue.png");
      }
      if(Numer==1)
      {
          setImage("button-green.png");
      }
      if(Numer==2)
      {
          setImage("button-purple.png");
      }
      if(Numer==3)
      {
          setImage("button-red.png");
      }
    }    
}
I have 5 classes of balls in different color. I want to assign different number to the balls. In my class shot i want to compere random number and ball number (when i hit a ball) if the number is the same i do something else do something else.
There are more replies on the next page.
1
2
3