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

2018/5/21

Greenfoot.stop() not working

GreenFootTeachet70 GreenFootTeachet70

2018/5/21

#
Do I have my method call in the wrong place or something. Put this in the Lobster class so that when the Lobster touches the Crab, the game ends. But it doesn't end.
public void act() 
    {
       turnAtEdge();
        if(Greenfoot.getRandomNumber(20)< 10)
        {
            turn(7);
        }
        move(7);
        endgame();
        lookForCrab();
        
       
        
        
    }
    
    public void lookForCrab()
    {
        if( isTouching(Crab.class) )
        {
            removeTouching(Crab.class);
            
        }
    }
    
    public void turnAtEdge()
    {
        if( isAtEdge() )
        {
            turn(17);
        }
    }
    
    public void endGame()
    {
        if ( isTouching(Crab.class) )
        {
            Greenfoot.stop(); 
            return;
        }
    }
    
}    

danpost danpost

2018/5/21

#
Please show the entire class. Also, show the entire Crab class as well.
jamesN616 jamesN616

2018/5/21

#
Try removing the endGame() method and add Greenfoot.stop(); on line 22. I'm not sure if that'll fix the problem or if the problem lies elsewhere, but might as well give it a shot.
GreenFootTeachet70 GreenFootTeachet70

2018/5/21

#
jamesN616 wrote...
Try removing the endGame() method and add Greenfoot.stop(); on line 22. I'm not sure if that'll fix the problem or if the problem lies elsewhere, but might as well give it a shot.
Yeah, when a crab comes into contact with the lobster the computer has two method calls that use isTouching()....the first one deletes the crab, the second one stops the program...but is it because the crab is now gone...it doesn't actually call the endGame() method?
GreenFootTeachet70 GreenFootTeachet70

2018/5/21

#
Here is the CrabWorld class...
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)

public class CrabWorld extends World
{
    /**
     * Create the crab world (the beach). Our world has a size 
     * of 560x560 cells, where every cell is just 1 pixel.
     */
    public CrabWorld() 
    {
        super(560, 560, 1);
        
    }
}
Here is the Crab class....
import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    public void act()
    {
        
        turnAtEdge();
        movement();
        lookForWorm();
        
        
        
    }
    
    public void lookForWorm()
    {
        if( isTouching(Worm.class) )
        {
            removeTouching(Worm.class);
            
        }
    }
    
    public void turnAtEdge()
    {
        if( isAtEdge() )
        {
            turn(17);
        }
    }
    
    public void movement()
    {
        if( Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }
        
        if( Greenfoot.isKeyDown("right"))
        {
            turn(4);
        }
        
        if( Greenfoot.isKeyDown("up"))
        {
            move(4);
        }
        
        if( Greenfoot.isKeyDown("down"))
        {
            move(-4);
        }
    }
    
    

}
Here is the Lobster class .... import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Lobster here. * * @author (your name) * @version (a version number or a date) */ public class Lobster extends Actor { /** * Act - do whatever the Lobster wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { turnAtEdge(); if(Greenfoot.getRandomNumber(20)< 10) { turn(4); } move(7); //lookForCrab(); endGame(); } //public void lookForCrab() //{ // if( isTouching(Crab.class) ) // { // // } //} public void turnAtEdge() { if( isAtEdge() ) { turn(17); } } public void endGame() { if ( isTouching(Crab.class) ) { removeTouching(Crab.class); Greenfoot.stop(); //return; } } } So the program now stops when the lobster touches the crab. I guess I will keep plodding on in my tutorial. Eventually, it must tell us how to have a collision and remove the object and replace it with another object.
danpost danpost

2018/5/21

#
GreenFootTeachet70 wrote...
So the program now stops when the lobster touches the crab. I guess I will keep plodding on in my tutorial. Eventually, it must tell us how to have a collision and remove the object and replace it with another object.
I am not sure that it will give both at the same time, but certainly it will at some point show adding an actor from within an Actor subclass (smoke for bouncy balls in Joy of Code videos).
You need to login to post a reply.