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

2012/12/28

Actor Lives

ctgreenfoot ctgreenfoot

2012/12/28

#
Hi there, so I'm on the final stage of my Little Crab game! But I was just wondering how I give an actor (in this case the Crab) lives. So for example if the Lobster ate the Crab, the Crab would simply "respawn" (relocate) in a different position and each time this occurred it would cause the Crab to lose a life. I would like the Crab to have a total of 5 lives. I've created a new actor named LifeCounter and here is the code so far:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class LifeCounter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LifeCounter extends Actor
{
    /**
     * Act - do whatever the LifeCounter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private int crabLives ; 
  
}
danpost danpost

2012/12/28

#
You need to add a constructor that calls a method to update the image for this actor; then add two more methods: one to get the current crabLives value and one to change the current crabLives value and update the image.
Peach Peach

2012/12/29

#
I'm trying to do the same. What would the methods be?
danpost danpost

2012/12/29

#
@Peach, what would you expect those methods to be?
Peach Peach

2012/12/29

#
I am not sure. I am new to greenfoot and programming
danpost danpost

2012/12/29

#
@Peach, it might be good for you to review the Java Trail; Learning the Java Language. The section on 'Classes and Objects' explains how to write methods. The section 'Language Basics' has most of what you will need to get started in programming.
You need to login to post a reply.