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

danpost's Comments

Back to danpost's profile

Cannot help without source code.
Last reminder, if anyone has downloaded this before the last update -- Please, re-download it to correct a snafu. (that is, if you do not have 'removeObject' methods in the SWorld class).
Just a reminder, if anyone has download this before the last update -- Please, re-download to correct a bug.
The method you are looking for is the 'act' method within the Snake class. All objects (world and actors) will each have a turn at their 'act' methods being executed. It is called one cycle (or frame) when all have executed once. There will be many cycles repeated every second, which is what puts 'life' into your world. The first statement (the 'if') in the 'act' method of the Snake class puts a condition on executing the code located there. It basically says, "Do not execute my code unless you are acting on the 'head' segment of the snake." (it actually says the same thing in the opposite sense - "Only if acting on the 'head' of the snake should you execute my code.") Inside the conditional block of code, starting from the back end of the snake, each segment is moved to the location of the segment in front of it; then, the 'head' of the snake (which is user-controlled) will move. If there are no changes (no keys pressed to change the direction of the 'head'), the 'head' will continue in the direction last instructed.
@hkrhässleholm, other than "head" being a character array of length 4 that are ordered 'h', 'e', 'a', 'd', all greenfoot can do is whatever processing is programmed for it. In this case, it will be set as the first parameter in the matching constructor in the Snake class (a variable called 'segment', in the constructor). From there, it is set to the Snake class instance variable with the same name ('this.segment=segment;' accomplishes that). Finally, in the 'act' method the value of the string is checked before executing the code there ('if(segment="head")'). Hope this clears things up.
danpostdanpost

2012/11/23

Actually, you do not even need a variable to hold the number of lives. You could just use: getWorld().getObjects(Lives.class).getSize() or, if in the world getObjects(Lives.class).getSize() to get the number of lives.
danpostdanpost

2012/11/23

All that needs done, is to make the NUMBER_OF_LIVES a non-static variable. This would mean making two simple modifications in the code. One is changing a static method to non-static; the other is changing 'CellarWorld.' to 'cw.' in one of the actor classes. Both these changes are directly related to the NUMBER_OF_LIVES variable.
@jabirfatah91 1. the "addArray" method basically increases the size of the 'snake' array 2. refer to the System class API for the low-down on 'arraycopy' http://docs.oracle.com/javase/7/docs/api/java/lang/System.html 3. the "<className>" indicates what type of objects will be stored in the list created; "List<Item>item=getObjects(Item.class);" creates a list object called 'item' to hold Item class objects and populates the list with the objects returned from the 'getObjects(Item.class)' call Item class: 1. why is"Wall.class" is used? Well, actually, it probably should have both checks in there; but, the Snake cannot go into the Wall object to 'eat' the Item class object; so, the 'act' method of the Item class relocates it until it is not located within a wall.
danpostdanpost

2012/11/22

Yes, a game over screen appears when you lose all your lives. However, when you complete all three levels you end up going back to the first level. Also, reseting the scenario does not replenish your lives.