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

Report as inappropriate.

Sniper presents ...



- Arrows or WASD to move
- Space or enter to shoot
- R or click the red button to restart

2603 views / 722 in the last 7 days

2 votes | 0 in the last 7 days

Tags: game with-source

open in greenfoot
Your browser does not support the canvas tag.


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)


You're welcome! :D
A new version of this scenario was uploaded on Sun Sep 29 06:59:05 UTC 2013 Added restart button (press r or click the red button)


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.

See all comments

Want to leave a comment? You must first log in.

Who likes this?

Entity1037 g3o