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

2019/3/18

Checking boundaries in my game

Sam123123 Sam123123

2019/3/18

#
Hello again, I was finally able to fix the issue with my bullet only firing to the right! but now I am having trouble with the checkinng boundaries. I added code to make the bullet disappears once it gets to the edge of the map, but when it reaches the end of the map the game freezes and gives me an error... Does sommebody know what I can do to fix this?
 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Mover
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
 
    public void act()
   {
       move(10.0);
       checkBoundaries();
       destroyEnemies();
   }
   //we add a method "checkBoundaries()" that destroys bullets that are off screen.
   public void checkBoundaries()
   {
       if(getY()>555||getX()>555 ||getY()<5||getX()<5 ) {
           getWorld().removeObject(this);
        }
   }
   //"destroyEnemies()" destroys enemies.
   public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy. 
       Actor enemy = getOneIntersectingObject(Vijand  .class);
       if(enemy != null) {
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
       }
   }
}
nccb nccb

2019/3/18

#
You must make sure that after you remove yourself from the world, you do not call getWorld() again or try to make any calls that involve interacting with the world like collision detection. One way to do this is to make checkBoundaries return a boolean indicating whether it was removed from the world, and if so, don't call destroyEnemies afterwards.
Sam123123 Sam123123

2019/3/19

#
nccb wrote...
You must make sure that after you remove yourself from the world, you do not call getWorld() again or try to make any calls that involve interacting with the world like collision detection. One way to do this is to make checkBoundaries return a boolean indicating whether it was removed from the world, and if so, don't call destroyEnemies afterwards.
Thank you for your reply nccb, but since I have honestly no clue how Greenfoot works, I don't know what to put in said Boolean. Could you maybe point me in the right direction of what I need to put in it.
nccb nccb

2019/3/19

#
This is what I was thinking of -- checkBoundaries returns true if still in the world, and only if this is true do you call destroyEnemies:
    public void act()
   {
       move(10.0);
       if (checkBoundaries())
           destroyEnemies();
   }
   //we add a method "checkBoundaries()" that destroys bullets that are off screen.  Returns true if still in world
   public boolean checkBoundaries()
   {
       if(getY()>555||getX()>555 ||getY()<5||getX()<5 ) {
           getWorld().removeObject(this);
           return false;
        }
        return true;
   }
Sam123123 Sam123123

2019/3/28

#
Thank you so much nccb! this worked perfectly!
You need to login to post a reply.