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

2013/2/8

Removing an object at world edge problem

1
2
Gingervitis Gingervitis

2013/2/8

#
I want my blaster bullets to be removed from the world once it is at the world egde but it stops the game when it is removed. How can I make it be removed without stopping the game? I used this code
public void ifAtWorldEdge()
    {
        if (atWorldEdge())
        {
             getWorld().removeObject(this);
        }
    }
danpost danpost

2013/2/8

#
Please supply the error message are you getting (copy/paste message) and also the code for your 'act' method of that class.
Gingervitis Gingervitis

2013/2/8

#
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. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Animal.canSee(Animal.java:32) at BlasterBullet.tryToEat(BlasterBullet.java:39) at BlasterBullet.act(BlasterBullet.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
 public void act() 
    {
        move(6);       
        ifAtWorldEdge();
        tryToEat();
        tryToEatGreenAlien();
        tryToEatBlueAlien();
        tryToEatPurpleAlien();
        

    }
danpost danpost

2013/2/8

#
The problem is due to the four 'tryToEat...' methods that need the object in the world to work. If 'ifAtWorldEdge' removes the object from the world, it will not find a world to look for food in. Either move 'ifAtWorldEdge' to the end of the 'act' method or place the following line in the code immediately following that method call:
if(getWorld() == null) return;
to exit the method and not execute any 'tryToEat...' methods if the object was removed from the world.
Gingervitis Gingervitis

2013/2/8

#
OHH.... ok I understand completely... Thank you! I have one more question. I am making an alien boss on the moon before my turtle returns back to earth. When the turtle shoots the AlienKing with the blaster bullets I want the AlienKing to eat the blaster bullets and after every 5 times it will create new small aliens and get smaller, but the AlienKing doesn't even eat the blaster bullets. Is it because the blaster bullets are really small and the Alien King is really big? I even wrote "eat(BlasterBullet3.class));" in the act method if it can see the class and it didn't work. The AlienKing has no problem eating other classes.
danpost danpost

2013/2/8

#
If your 'eat' method uses the 'getObjectsAtOffset' method, then the blaster bullets would have to intersect the center of the large AlienKing object to be detected. However, if your 'eat' method uses one of the '...Intersecting...' methods then the bullet could be anywhere on the AlienKing object and be detected. This is the only way that the difference in size could make a difference.
danpost danpost

2013/2/8

#
Your 'canSee' method would also need changed (if it is using 'getObjectsAtOffset').
Gingervitis Gingervitis

2013/2/8

#
I never used 'getObjectsAtOffSet' before. W
public void tryToEat()
    {
        if (canSee(BlasterBullet2.class))
        {
            eat(BlasterBullet2.class);            
        }

        if (canSee(SpaceTurtle2.class))
        {
            
            eat(SpaceTurtle2.class);
        }
    }
hat would I change in my 'tryToEat' method?
danpost danpost

2013/2/8

#
The Animal class is using 'getOneObjectAtOffset' in both the 'canSee' and the 'eat' methods that the 'tryToEat' method calls. You can either change the type of method used in those classes, or you can write you own collision detection in the 'tryToEat' method (so you do not mess with a class someone else wrote).
public void tryToEat()
{
    BlasterBullet2 bb2 = getOneIntersectingObject(BlasterBullet2.class);
    if (bb2 != null) getWorld().removeObject(bb2);
    SpaceTurtle2 st2 = getOneIntersectingObject(SpaceTurtle2.class);
    if (st2 != null) getWorld().removeObject(st2);
}
Gingervitis Gingervitis

2013/2/8

#
I got an error saying "incompatible types- found greenfoot.Actor but expected BlasterBullet2 at line 3 above. Is it because the BlasterBullet2 class is in the mover subclass?
danpost danpost

2013/2/8

#
Sorry, forgot to cast the returns, but I think putting them in Actor fields is enough.
public void tryToEat()
{
    Actor bb2 = getOneIntersectingObject(BlasterBullet2.class);
    if (bb2 != null) getWorld().removeObject(bb2);
    Actor st2 = getOneIntersectingObject(SpaceTurtle2.class);
    if (st2 != null) getWorld().removeObject(st2);
}
Gingervitis Gingervitis

2013/2/8

#
How can I put in the statement
bulletsEaten = bulletsEaten + 1;
            
            if (bulletsEaten == 5)
            {
                createNewGreenAlien();
            }
if there are no curly brackets in the if statement?
danpost danpost

2013/2/8

#
You can add them in:
public void tryToEat()
{
    Actor bb2 = getOneIntersectingObject(BlasterBullet2.class);
    if (bb2 != null)
    {
        getWorld().removeObject(bb2);
        bulletsEaten++;
        if (bulletsEaten == 5) createNewGreenAlien();
    }
    Actor st2 = getOneIntersectingObject(SpaceTurtle2.class);
    if (st2 != null) getWorld().removeObject(st2);
}
Gingervitis Gingervitis

2013/2/8

#
Oh ok. I was unsure where and If I could still add the curly brackets to the statement. Thank you.
Gingervitis Gingervitis

2013/2/9

#
I am am getting a new error, similar to the one I posted yesterday. This is the error message. 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. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Animal.canSee(Animal.java:32) at SpaceShipDebris5.tryToEatAlien(SpaceShipDebris5.java:49) at SpaceShipDebris5.act(SpaceShipDebris5.java:36) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) Here is my code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SpaceShipDebris5 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceShipDebris5 extends SmoothMover
{
    private static final Vector GRAVITY = new Vector(-180,1.5);
    private static final int FORCE = 15;
       
    public SpaceShipDebris5()
    {
        int direction = Greenfoot.getRandomNumber(360);
        int speed = Greenfoot.getRandomNumber(FORCE);
        increaseSpeed( new Vector(direction, speed));
    }
    /**
     * Act - do whatever the SpaceShipDebris wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        increaseSpeed(GRAVITY);
        move();
        tryToEatAlien();
    } 
    
    public void tryToEatAlien()
    {
        if (canSee(GreenAlien.class))
        {
            eat(GreenAlien.class);                      
        }
   
        if (canSee(BlueAlien.class))
        {
            eat(BlueAlien.class);      
        }
    
        if(canSee(PurpleAlien.class))
        {
            eat(PurpleAlien.class);   
        }
    }
   
}
There are more replies on the next page.
1
2