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

2012/3/4

Game Help

Dom Dom

2012/3/4

#
How do I make my actor die, if he falls down a hole, to the bottom of the world?
danpost danpost

2012/3/4

#
You basically said what you wanted to code: "If my actor is at the bottom of the world, then remove it."
if (getY() == getWorld().getHeight() - 1)
{
    getWorld().removeObject(this);
    return;
}
Put this in your actor class (the one that dies) in the method that makes him fall, right after the setLocation() or move() statement. If it is in a move() method, you may have to add a line right after the calling statement in the act() method 'if (getWorld() == null) return;'.
Dom Dom

2012/3/4

#
How do I create: 1) Menus, 2) Scorecounters 3) Livescounters If anyone could just shed some light on these?
danpost danpost

2012/3/4

#
There subjects often come up on the discussion board. There is much information to be gained from them. Do a search on them and check them out.
Dom Dom

2012/3/4

#
I had a look and I cant find the exact answer I'm looking for, for any of these items. I need a simple explanation of how to create each of these, and I cant find that.
danpost danpost

2012/3/4

#
The menu one is by far the hardest one to create. You can use re-usable sub-classes (copy/paste) for this one (go here).The other two are, simply put, a matter of creating Actor subclasses with instance integer counters and putting 'updateImage()' methods in them to create and set the current image needed (and public methods for changing the counts).
Dom Dom

2012/3/4

#
Could you give me the coding for lives and score counters?
danpost danpost

2012/3/4

#
A simple Scoreboard class would be something like:
import greenfoot.*;
import java.awt.Color;

public class Scoreboard extends Actor
{
    int score = 0;
    
    public Scoreboard()
    {
        updateBoard();
    }
    
    private void updateBoard()
    {
        setImage(new GreenfootImage("Score: " + score, 20, Color.black, new Color(0, 0, 0, 0)));
    }
    
    public void add(int addVal)
    {
        score += addVal; updateBoard();
    }
    
    public int getScore()
    {
        return score;
    }
}
danpost danpost

2012/3/4

#
You can use the same code for the lives counter with minor modifications and some extra code to check for 'no life remaining':
import greenfoot.*;
import java.awt.Color;

public class Lifeboard extends Actor
{
    int lives = 0;
    
    public Lifeboard()
    {
        updateBoard();
    }
    
    private void updateBoard()
    {
        setImage(new GreenfootImage("Lives remaining: " + score, 20, Color.black, new Color(0, 0, 0, 0)));
    }
    
    public void add(int addVal)
    {
        if (lives == 0 && addVal == -1)
        { // ^ (lost a life and no lives remaining)
            // play sound, showGameOver/Final score
            // whatever you want to do for ending game
            Greenfoot.stop();
            return;
        }
        lives += addVal; updateBoard();
    }
    
    public int getLivesLeft()
    {
        return lives;
    }
}
Dom Dom

2012/3/7

#
For the lives code, when i tried to compile it, it said that 'cannot find symbol - variable score'
   private void updateBoard()
    {
        setImage(new GreenfootImage("Lives remaining: " + score, 20, Color.black, new Color(0, 0, 0, 0)));
    }
danpost danpost

2012/3/7

#
I missed that one in re-writing it for that class. Change the work 'score' in line 3 of your last post to 'lives'.
Dom Dom

2012/3/7

#
how do i link my actor to this now?
danpost danpost

2012/3/11

#
Set up a reference to the Scoreboard and Lifeboard in the world class when you create them, and store them in world class instance Object variables
public Scoreboard scoreboard = new Scoreboard();
public Lifeboard lifeboard = new Lifeboard();
Then in the world constructor, or a method that it calls, add them to the world
addObject(scoreboard, 100, 20);
addObject(lifeboard, 500, 20);
Of course, the location to add them is of your choice. Now, in the actor class, when you need to adjust the score, create a reference to the world it is in (let us just say that you named your world sub-class 'Arena') and access the lifeboard and scoreboard from there.
Arena arena = (Arena) getWorld();
Scoreboard sBoard = arena.scoreboard;
sBoard.add(10); // adds 10 to the score
arena.scoreboard.add(10);
You need to login to post a reply.