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

2015/4/7

Shooting Causes Greenfoot To Pause

1920 1920

2015/4/7

#
I've made a game where when you shoot depending on the enemy they are either removed from my world or they spawn somewhere else. But every time I use the space bar to shoot when I run the game, my game gets paused when bullets hit edge or enemies except the skull. IDK how to fix this everything else works Here's my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Shoot here. * * @author (your name) * @version (a version number or a date) */ public class Shoot extends Mover { /** * Act - do whatever the Shoot wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(10); if (hitTheEdge() !=null) { World myWorld = getWorld(); myWorld.removeObject(this); } shootRobots(); shootSkulls(); } public void shootRobots() { Actor dolphin = getOneIntersectingObject(Dolphin.class); if(dolphin != null) { World myWorld = getWorld(); myWorld.removeObject (dolphin); myWorld.removeObject(this); } } public void shootSkulls() { Actor baby = getOneIntersectingObject(Baby.class); if(baby != null) { World myWorld = getWorld(); myWorld.removeObject (baby); myWorld.removeObject(this); int randomX = (int)(Math.random() * myWorld.getWidth()); int randomY = (int)(Math.random() * myWorld.getHeight()); myWorld.addObject(baby, randomX, randomY); } } public String hitTheEdge() { int x = getX(); int y = getY(); World myWorld = getWorld(); int rightSide = myWorld.getWidth() - 1; int bottomSide = myWorld.getHeight() - 1; if (y == 0) { return "top"; } else if (x == 0) { return "left"; }else if (x == rightSide) { return "right"; }else if (y == bottomSide) { return "bottom"; } else { return null; } } }
lordhershey lordhershey

2015/4/7

#
you have a routine called shootRobots(); that can remove the actor from the world then you call shootSkulls(); which could try to manipulate the actor (this) that has been removed from the world. Also if you hit the edge you probably do not want to call these routines either. proably want something that looks more like this:
 public void act() 
    {
        move(10);
        
        if (hitTheEdge() !=null)
         
         
        {
            World myWorld = getWorld();
            myWorld.removeObject(this);
            return;
        }
        if(shootRobots())
        {
          return;
        }
        shootSkulls();
    }
    
    public boolean shootRobots()
    {
        Actor dolphin = getOneIntersectingObject(Dolphin.class);
        if(dolphin != null)
        
        {
            World myWorld = getWorld();
            myWorld.removeObject (dolphin);
            myWorld.removeObject(this);
            return true;
        }  
    return false;
    }
1920 1920

2015/4/7

#
@lordhershey Wow thank you, now it works with no problem :)
You need to login to post a reply.