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

2014/8/25

Best way to display text

GreenGoo GreenGoo

2014/8/25

#
I need to display at least three things at once during my game, and I was wondering what the neatest way to do that was. Obviously I could just generate lots of GreenfootImages during each act (they need to update every act) but that seems a very bad way to do it. Any suggestions?
Super_Hippo Super_Hippo

2014/8/25

#
GreenGoo wrote...
...(they need to update every act)...
What do you want to do? You can either print the text on the background, but then, you don't have control over it. Usually I create a 'Text' class (or something similar), so I have actors which display text. Then I can remove them or check if they are clicked etc.
GreenGoo GreenGoo

2014/8/25

#
I tried that as well. My approach was to store some variables in my World, then reference them in my TextDisplayer constructor. But then the output is fixed to what the variable was when the object was created. It might be that this way is totally rubbish, so I'd appreciate some code to see what you mean.
Super_Hippo Super_Hippo

2014/8/25

#
If you are as far to display text which you passed in the constructor, you can just create a 'setText' method or something like that to adjust the text. To change the text, you only need a reference to this object and call the 'setText' method and pass the new text.
danpost danpost

2014/8/25

#
GreenGoo wrote...
I could just generate lots of GreenfootImages during each act (they need to update every act) but that seems a very bad way to do it.
Is this scrolling text or color changing text? Otherwise, I do not see why you would have to update it every act. Maybe you should show what you are trying to do with some code and explain why it needs updated every act.
GreenGoo GreenGoo

2014/8/25

#
Here is my World:
import greenfoot.*;

public class Background extends World
{
    int squareCount, powerCount, score, spawnX, spawnY;
    int numberOfLives = 3;
    boolean allRed = false;

    public Background()
    {    
        super(900, 600, 1, false); 
        addObject(new MousePlayer(), getWidth() / 2, getHeight() / 2);
    }

    public void act() 
    {
        spawnSquares();
        spawnPowers();
    }

    public void spawnSquares() 
    {
        if (squareCount <= 49) {
            int sideChoose = Greenfoot.getRandomNumber(4);
            int paintID = Greenfoot.getRandomNumber(2);
            int size = Greenfoot.getRandomNumber(20) + 10;
            switch(sideChoose) {
                case 0: spawnX = getWidth() + 25;
                spawnY = Greenfoot.getRandomNumber(getHeight());
                break;
                case 1: spawnX = Greenfoot.getRandomNumber(getWidth());
                spawnY = getHeight() + 25;
                break;
                case 2: spawnX = -25;
                spawnY = Greenfoot.getRandomNumber(getHeight());
                break;
                case 3: spawnX = Greenfoot.getRandomNumber(getWidth());
                spawnY = -25;
                break;
            }
            addObject(new Square(paintID, sideChoose, size), spawnX, spawnY);  
            squareCount++;
        } 
    }
Not sure if you need that, but it can't hurt. Essentially, I want to display the numberOfLives in-game. It has to be updated every act because I always lose lives, and the player needs to see how many he or she has remaining. I like the idea of a setText() method. I'll give that a go. Hopefully that clears things up. I'll write a constructor now.
GreenGoo GreenGoo

2014/8/25

#
Well that was embarrassingly easy. Oops. Thanks for the help anyway!
Super_Hippo Super_Hippo

2014/8/25

#
If you only have 3 lives at the beginning and you 'always' lose lives, then your game is over very fast. :) By the way, maybe the 'Counter' class which you can import, could help you to display the number of lives.
GreenGoo GreenGoo

2014/8/25

#
It's very difficult on a laptop!
danpost danpost

2014/8/25

#
GreenGoo wrote...
It's very difficult on a laptop!
What do you mean?
GreenGoo GreenGoo

2014/8/25

#
I was replying to Super_Hippo, when he said that my game is over very fast because I 'always' lose lives. It would be a lot easier with a mouse.
danpost danpost

2014/8/25

#
I think Super_Hippo was thinking that if you start with 3 lives and lose a life every act, then you would be dead after three act cycles (about 1/20 seconds). You only need to update the lives counter and image when a life is lost; not every act cycle.
You need to login to post a reply.