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

2015/4/6

The Game Won't Stop??

alwanammar alwanammar

2015/4/6

#
Hello, i am making a simple game where bugs eats leaf. But the game won't stop after the leaves were all eaten. I suspect the counter isn't working. here's the code Superclass "Actors"
import greenfoot.*;

/**
 * Write a description of class Actors here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Actors extends Actor
{
    int LeafEaten = 0;
    /**
     * Act - do whatever the Actors wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void getEaten()
    {
    Actor  Bugs = getOneObjectAtOffset(0,0, Bugs.class);
    if (Bugs != null)
    getWorld().removeObject(this);
    LeafEaten=LeafEaten + 1;
    }
    public void Touching()
    {
        Actor Lizard = getOneObjectAtOffset(0, 0, Bugs.class);
        if (Lizard != null) Greenfoot.stop();
    }
   int i = Greenfoot.getRandomNumber(90);
    public void LizardBounce()
    {
        turn(i);
        getImage().mirrorVertically();
    }
    public void BugsTurn()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-3);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(3);
        }
    }
    public void gameOver()
    {
        {
        if(LeafEaten==9)
        Greenfoot.stop();
        }
    }
}
Subclass "Leaf"
import greenfoot.*;

/**
 * Write a description of class Leaf here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Leaf extends Actors
{
    /**
     * Act - do whatever the Leaf wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        getEaten();
        gameOver();
    }    
}
help me please, i am a newbie here :)
winnerpig winnerpig

2015/4/6

#
The “LeafEaten” is just a field variable (whose scope is in the every leaf object ),not static variable (whose scope is the whole Leaf class)。 Thus ,you could change the code of Actors in Line 11 to
public static int LeafEaten = 0;
alwanammar alwanammar

2015/4/6

#
winnerpig wrote...
The “LeafEaten” is just a field variable (whose scope is in the every leaf object ),not static variable (whose scope is the whole Leaf class)。 Thus ,you could change the code of Actors in Line 11 to
public static int LeafEaten = 0;
still wont work. the game wont stop. and one more thing, whenever i run the program. it paused once and i need to re-run again to start the game smoothly.
Super_Hippo Super_Hippo

2015/4/6

#
In the Actors class, you increase the LeafEaten variable if it intersects with an object of the Bug class. But this intersecting removes the object of the Actors class from the world. So LeafEaten is 0 for every Actors object in the world. It turns 1 when it is removed from the world. If you want to have a user controlled object which eats leaves, then you should put the leafEaten variable in the class which actually eats the leaves. Then you can also check for this variable to be 9 to end the game. If you have leaves and bugs (more than 1) moving randomly and you want to stop the simulation when 9 leaves were eaten, you should either put the leafEaten into the world class or 'static' in the class which eats the leaves (the bugs in your case). By the way, the LizardBounce method will turn the object by a fixed number (i is defined outside the method) and mirror the image. I don't think that this is really wanted.
alwanammar alwanammar

2015/4/6

#
Super_Hippo wrote...
In the Actors class, you increase the LeafEaten variable if it intersects with an object of the Bug class. But this intersecting removes the object of the Actors class from the world. So LeafEaten is 0 for every Actors object in the world. It turns 1 when it is removed from the world. If you want to have a user controlled object which eats leaves, then you should put the leafEaten variable in the class which actually eats the leaves. Then you can also check for this variable to be 9 to end the game. If you have leaves and bugs (more than 1) moving randomly and you want to stop the simulation when 9 leaves were eaten, you should either put the leafEaten into the world class or 'static' in the class which eats the leaves (the bugs in your case). By the way, the LizardBounce method will turn the object by a fixed number (i is defined outside the method) and mirror the image. I don't think that this is really wanted.
i put the LeafEaten to the Bugs.class as you say, but whenever my bugs eat the leaf, the game stopped -___- here's the code Superclass Actors
import greenfoot.*;

/**
 * Write a description of class Actors here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Actors extends Actor
{
       /**
     * Act - do whatever the Actors wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void getEaten()
    {
    Actor  Bugs = getOneObjectAtOffset(0,0, Bugs.class);
    if (Bugs != null)
    getWorld().removeObject(this);
    }
    public void Touching()
    {
        Actor Lizard = getOneObjectAtOffset(0, 0, Bugs.class);
        if (Lizard != null) Greenfoot.stop();
    }
    public int i = Greenfoot.getRandomNumber(90);   
    public void LizardBounce()
    {
        turn(i);
        getImage().mirrorVertically();
    }
    public void BugsTurn()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-3);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(3);
        }
    }
}
Bugs subclass
import greenfoot.*;

/**
 * Write a description of class Bugs here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bugs extends Actors
{
    public int LeafEaten = 0;
    /**
     * Act - do whatever the Bugs wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(2);
        BugsTurn();
        {
            if(this.isTouching(Leaf.class))
            LeafEaten = LeafEaten + 1;
        }
        {
            if(LeafEaten==9)
            gameOver();
        }
    }
    public void gameOver()
    {
        {
        Greenfoot.stop();
        }
    }
}
btw thanks for the bounce!
Super_Hippo Super_Hippo

2015/4/6

#
You could remove the 'getEaten' method and use the following in the Bugs class (which probably should be 'Bug' class).
private int leafEaten = 0;

public void act()
{
    int t=0;
    if(Greenfoot.isKeyDown("left")) t-=3;
    if(Greenfoot.isKeyDown("right")) t+=3;
    if (t!=0) turn(t);
    move(2);
    if (isTouching(Leaf.class))
    {
        removeTouching(Leaf.class);
        leafEaten++;
        if (leafEaten==9) gameOver();
    }
}
winnerpig winnerpig

2015/4/6

#
winnerpig wrote...
The “LeafEaten” is just a field variable (whose scope is in the every leaf object ),not static variable (whose scope is the whole Leaf class)。 Thus ,you could change the code of Actors in Line 11 to
public static int LeafEaten = 0;
by the way,you should reset the LeafEaten to zero by adding one line in the constructor of your world class,like this:
Actors.LeafEaten=0;
alwanammar alwanammar

2015/4/7

#
Super_Hippo wrote...
You could remove the 'getEaten' method and use the following in the Bugs class (which probably should be 'Bug' class).
private int leafEaten = 0;

public void act()
{
    int t=0;
    if(Greenfoot.isKeyDown("left")) t-=3;
    if(Greenfoot.isKeyDown("right")) t+=3;
    if (t!=0) turn(t);
    move(2);
    if (isTouching(Leaf.class))
    {
        removeTouching(Leaf.class);
        leafEaten++;
        if (leafEaten==9) gameOver();
    }
}
this code works to me. loool. though i still changed it to methods in superclass. big thanks brother! but the Leaf class left empty with no code?? btw, can i just made a statement like " if leaf is empty then greenfoot stop" ??
winnerpig winnerpig

2015/4/7

#
alwanammar wrote...
btw, can i just made a statement like " if leaf is empty then greenfoot stop" ??
obviously you can 。You can replace Line 14 to :
if(getWorld().getObjects(Leaf.class)==null) gameOver();
danpost danpost

2015/4/7

#
winnerpig wrote...
alwanammar wrote...
btw, can i just made a statement like " if leaf is empty then greenfoot stop" ??
obviously you can 。You can replace Line 14 to :
if(getWorld().getObjects(Leaf.class)==null) gameOver();
The 'getObjects' method will never return a null value. It always returns a List object; however, the list may not contain any elements. So, check if it is empty:
if (getWorld().getObject(Leaf.class).isEmpty()) gameOver();
winnerpig winnerpig

2015/4/7

#
OK,I ignored it ,which is equivalent to:
if (getWorld().getObject(Leaf.class).size()==0) gameOver();
You need to login to post a reply.