A new version of this scenario was uploaded on Sun Sep 29 00:20:43 UTC 2013
Added lives
I'm still looking for a way to respawn Tux when the lives > 0 and Tux doesn't exist. Any help is appreciated
Add a 'get' method to the Lives class to return the current value. Then in the world, add an act method that checks for a Tux object in world. If not in world, decrement the lives counter and check its value against zero. If zero, stop the scenario; otherwise, add a new Tux object into the world.
Could you post examples of that code? Sorry, I'm new.... Every way I've tried I get some kind of compiling error. Would I use getObjects(Tux.class) to find if he exists or?
I've tried doing this in the Lives.class but I get errors compiling whether I put = null, = 0, != 1, etc.... What am I doing wrong?
public void act()
{
if (totalLives > 0)
{
if (getWorld().getObjects(Tux.class) != 1)
{
Tux tux = new Tux();
getWorld().addObject(tux, 300, 300);
}
}
else
{
Greenfoot.stop();
}
}
A new version of this scenario was uploaded on Sun Sep 29 05:55:28 UTC 2013
Added respawn and new sounds :)
Try using:
[code]
getOneIntersectingObject("[Insert Name Here].class");
[/code]
instead of:
[code]
getOneObjectAtOffset(0,0,[Insert Name Here].class);
[/code]
It will detect an apple intersecting with the penguin's image instead of the center of the penguin.
A new version of this scenario was uploaded on Sun Sep 29 06:23:49 UTC 2013
Added winning/losing text/sounds
A new version of this scenario was uploaded on Sun Sep 29 06:29:31 UTC 2013
Tweaked apple detection code (thanks Entity1037)
Referring back to Lives and respawning Tux, the code is better off in the world class, however, it should work still in the Lives class. Looking at the greenfoot.Actor class documentation, you will see that 'getObjects' returns a List object and therefore cannot be compared to an integer and will never be null. Looking at the methods in the java.util.List class documentation, you will find an 'isEmpty' method which you can make use of.
2013/9/28
2013/9/29
2013/9/29
2013/9/29
2013/9/29
2013/9/29
2013/9/29