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

2012/3/15

Update on Live Scores...

Omniscience Omniscience

2012/3/15

#
After racking my brain, I still haven't been able to get a good solution to this whole live score problem. The situation is that I need an integer to be converted to a string every so many cycles. This string needs to be drawn onto a fancy image representing the Actor subclass in question (which contains a space for the player's score, that increments over time); this image is saved onto the World. The main issue is that the drawn string needs to be cleared each time before for the next string is drawn onto the image. Otherwise, the strings overlap and it all looks messy. I also need help setting things to do with the font, like size and colour. In short: An integer value increases after set intervals of time. At each interval, that integer must be converted to a String, and be drawn onto the Actor's image. In addition, the font needs to be set, the colour of the font, and its size. Here's my progress so far (not that it works...) :
 private int score = 0; 
    private int scoreCount = 0;
    private String scoreValue = "";
    public void act() {
        scoreCount++;
        if (scoreCount % 20 == 0) {
            incrementScore();
        }
    }

    private void incrementScore() { 
        score++;
        setColor(Color.red); 
        Font arialFont = new Font(arial, Font.BOLD, 16);
        setFont(arialFont);        
        scoreValue = Integer.toString(score);
        this.getImage().drawString(scoreValue, 10, 10);
    }
As usual, all help is welcome, and much appreciated!
Duta Duta

2012/3/15

#
Why not something along these lines:
private int scoreCount = 0;

public void act() {
	scoreCount++;
	if (scoreCount % 20 == 0)
		incrementScore();
}

private void incrementScore() {
	String text = "" + ((int)(scoreCount/20));
	int fontSize = 20;
	Color textColor = Color.RED,
		bgColor = new Color(0, 0, 0, 0); //Transparent.
	setImage(new GreenfootImage(text, fontSize, textColor, bgColor));
}
Obviously you can fiddle with the details and all
Omniscience Omniscience

2012/3/16

#
Wouldn't this just swap the image I've defined for the Actor subclass for this newly defined image? I want it so that the score is drawn over the existing image... But hey, I'll give it a try. Thanks Duta!
sp33dy sp33dy

2012/3/16

#
It's best to use a second actor to write to and place it over the top of your game/image. In that way, as the score changes, you don't end up corrupting the image beneath (I assume you knew this, but stating just to make sure). The method Duta provides above is tapping into creating a new Image that has text writing function; thereby creating a new image with the text in. Placing this into the current actor and thereby perfect for placing over the game. You can then keep updating the score with out fear of disrupting the pixels underneath it! Make sure you place it into the world after your game images, otherwise it might get obscured if it is behind others!!
davmac davmac

2012/3/16

#
Another way to do it would be to always keep a copy of the original image; the process of updating it would then be: - create a new image which is a copy of the original (use the copy constructor of GreenfootImage - the constructor that takes another image as an argument) - draw the text onto the copy - set the copy as the image for the actor (setImage(...))
Omniscience Omniscience

2012/3/16

#
@sp33dy I have considered doing that, but I was concerned another class may reduce the efficiency of my program? :s @davmac So essentially this diverts the threat of corrupting the original image. Gotcha. I was wondering, however, when I draw the text onto the image (be it copy or not), that image always disappears, yet the score remains... any help as to why?
sp33dy sp33dy

2012/3/16

#
It's great that you are thinking about the performance of your program! That's always a good sign of sensible programming, often overlooked by people. Davmac's approach is also a sensible one but would result in a similar sized amount of memory being consumed. The difference is, in the later, you would continually be copying image data from one image to another. In my view (I could be wrong), allocating one new actor with a dedicated image that your could 'clear' and then write directly on (not using the constructor method) is the most efficient approach. What you really want to avoid is continually creating new images, for which the old ones have to be garbage collected. In my opinion, creating new classes/actor's shouldn't be avoided, if they give you a simple solution. What I mean here is that the class is dedicated to writing just the score text to it, for which is displayed; as opposed to having to write code that copy's the bitmap back in and then you stamp your text over the top. Although, if you are continually drawing the background image first, then writing the text directly over it is actually better! Overall, I've probably confused myself as well as you with this post! My simple advice is this: 1) Get it working simply 2) Refactor the code into the best performing solution. There are many techniques for testing how quick your code works. You then tune until satisfied. I hope this helps. Do feel free to ignore.. :-)
Omniscience Omniscience

2012/3/16

#
@sp33dy You have totally persuaded me to do it to the letter of your advice. And by the way, the only novice on this thread is me, and seeing as you have been doing this for such a long time, I am nothing but obliged to take your advice. :)
Omniscience Omniscience

2012/3/16

#
Thankyou very much, all that were able to help! I now have the score working. What I've been trying to do is now set the font style and size... here is the code I have:
private void incrementScore(){
        image.clear();
        Font font = new Font("Verdana", Font.BOLD, 25);
        String text = ("" + score);
        text.setFont(font);
        image.drawString(text, 1, 45);
        setImage(image);
}
Although I have imported java fonts, when I compile the code, I'm told that it can't find setFont(). Any ideas as to how I can fix this, so my text will be bold, with size 25 pixels?
sp33dy sp33dy

2012/3/16

#
Hi, No need to thank, I'm just pleased to help. You are trying to call setFont on the String object text. You need to setFont on a GreenfootImage I believe, so image.setFont(font) in your case.
You need to login to post a reply.