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

2019/5/23

Help using showtext and simple integers

CROCKETTROCKETT4 CROCKETTROCKETT4

2019/5/23

#
Hello, I'm trying to keep track of a score for my game, and my current code is showing some weird message like "Object@2jksndg2" something or other. What is wrong with it? Thank you! First code snippet is from the World section (constructor), and the second is in an Actor
        addObject(new Object(), 200, 200);
        //addObject(new Object(), 100, 200);
        updateImage();
        Greenfoot.setSpeed(50);
        Greenfoot.playSound("fortunateson.mp3");
        Object score = new Object();
        String scoreAsString = String.valueOf(score);
        //String scoreAsString = new Integer(score).toString();
        showText(scoreAsString, 350, 20); 
private void checkSoldierdHit()
    {
        Soldier soldier = (Soldier) getOneIntersectingObject(Soldier.class);
        
        if (soldier != null){
            getWorld().removeObject(soldier);
            Greenfoot.playSound("yee.mp3");
            score += 1;
            
        }         
    }
    public int getScore()
    {
        return score;
    }
(just fyi, our int score = 0; is in the actor)
danpost danpost

2019/5/23

#
If you named one of your classes Object -- don't. At least, it is not a good idea to as Object is the root class of ALL classes. Not sure why you are adding this object into your world or why you create another one named score -- as this will not have anything to do with the score field in your Actor subclass. A simple way to convert an int to a String is as follows:
String scoreAsString = ""+score;
although the following ways will also work.
String scoreAsString = String.valueOf(score);
// and
String scoreAsString = (new Integer(score)).toString();
// and
String scoreAsString = Integer.toString(score);
You should have the actor with the score field show the text in the world it resides within using:
getWorld().showText(""+score, 350, 20);
CROCKETTROCKETT4 CROCKETTROCKETT4

2019/5/29

#
I owe you my life. Thank you Danpost
You need to login to post a reply.