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

2018/4/14

Game stops when my lazer hits an enemy.

Daanfaverey Daanfaverey

2018/4/14

#
I'm using greenfoot for a schoolproject. That's the reason why i'm asking this much questions :). My sonic can shoot lazers. First when my sonic did shoot lazers the lazers would get stuck at the right edge of the world. But if they hit my enemy1 (in the game called 'Vijand1') they would disappear. Now i found a way to remove the lazers that got stuck on the right side of the map. But when a lazer hits my enemy1 (Vijand1) the game stops. How do I fix this?
    public void act() 
    {
         if (Greenfoot.isKeyDown("left")) {
            setRotation(180);
            move(3);
            
        }
        if (Greenfoot.isKeyDown("up")) {
            setRotation(270);
            move(3);
        }
        if (Greenfoot.isKeyDown("right")) {
            setRotation(0);
            move(3);
        }
        if (Greenfoot.isKeyDown("down")) {
            setRotation(90);
            move(3);
        }        
             tryToEat();
        if (Greenfoot.isKeyDown("space")) {
            World world = getWorld();
            Lazer Lazer = new Lazer();
            Lazer.setsonic(this);
            world.addObject(Lazer, getX(), getY());
        }
     }
     public void tryToEat()
     {
         if (canSee(muntje.class) )
         {
             eat(muntje.class);
             muntjeGegeten++;
             Greenfoot.playSound ("Ooh he stealing!.mp3");
             World1 world = (World1)getWorld(); //casting
             world.updateScore();
            
         }
        
    }    
    public void checkVijand1()
    {
        if (canSee(Vijand1.class) )
        {
             Greenfoot.playSound("game-over.wav");
             Greenfoot.stop();
        }
    } 
}
This is my sonic code ^^^ public boolean atWorldEdge() { if (getX() > getWorld().getWidth() - getImage().getWidth() || getY() > getWorld().getHeight() - getImage().getHeight()) { return true; } else { return false; } } /** * Set the objectreference to the gardener * @param g The objectreference to the gardener */ public void setsonic (dier s) { sonic = s; } /** * Move forward, not to slowly */ public void act() { move(6); if (isTouching (Vijand1.class)) { Vijanden Vijand1 = (Vijand1)getOneIntersectingObject (Vijand1.class); World world = getWorld(); world.removeObject (this); } { if (this.atWorldEdge()==true) { World world; world = getWorld(); world.removeObject(this); return; } } } This is my lazer code^^ public void act() { move(3); randomTurn(); turnAtEdge(); } /** * With a 10% probability, turn a bit right or left. */ public void randomTurn() { if ( Greenfoot.getRandomNumber(100) < 10 ) { turn( Greenfoot.getRandomNumber(40)-20 ); } } /** * If we reach the edge of the world, turn a little bit. */ public void turnAtEdge() { if (atWorldEdge()) { turn(7); } } } and this is my Enemy1 code
danpost danpost

2018/4/14

#
Daanfaverey wrote...
I'm using greenfoot for a schoolproject. That's the reason why i'm asking this much questions :). My sonic can shoot lazers. First when my sonic did shoot lazers the lazers would get stuck at the right edge of the world. But if they hit my enemy1 (in the game called 'Vijand1') they would disappear. Now i found a way to remove the lazers that got stuck on the right side of the map. But when a lazer hits my enemy1 (Vijand1) the game stops. How do I fix this? << Code Omitted >>
In the sonic class act method, move the return statement (in the last if block) to the end of the preceding if block.
You need to login to post a reply.