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

2017/11/4

display text with variable

ZigZagNinja ZigZagNinja

2017/11/4

#
so in the game i have creaed so far you can collect coins i already have a variable counting the collected coins all i want is to display them on the map while playing. Is there any way of doing this??
Super_Hippo Super_Hippo

2017/11/4

#
Easiest way is probably the showText method in the world class. Another way is to import the counter class and save the variable there instead of wherever you have it right now.
ZigZagNinja ZigZagNinja

2017/11/5

#
Could someone maybe give me an example?
Super_Hippo Super_Hippo

2017/11/5

#
getWorld().showText(variableName, /**somewhere*/, /**somewhere*/);
For the second way, the counter class has a description you can follow.
ZigZagNinja ZigZagNinja

2017/11/5

#
ok thank you
ZigZagNinja ZigZagNinja

2017/11/5

#
now i have an error saying : cannot find symbol - method getWorld() this is my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class GameWorld extends World { private int steinSpawn = 180; private int steinSpawnTimer = steinSpawn; private int muenzenSpawn = 180; private int muenzenSpawnTimer = muenzenSpawn; public GameWorld() { super(600, 400, 2); setBackground("grass.jpg"); //neuen Spieler erstellen Spieler neuerSpieler = new Spieler(); //neuen Spieler in die Welt setzen addObject (neuerSpieler, 10, 200); } public void act() { // jede paar sekunden wird zufällig irgendwo ein Stein erstellt zufälligSteineErstellen(); // jede paar sekunden wird zufällig irgendwo eine Muenze erstellt zufälligMuenzenErstellen(); getWorld().showText(muenzenSpawnTimer, 10, 10); } //es wird jede 5 Sekunden ein neuer Stein hinzugefügt public void zufälligSteineErstellen() { if (--steinSpawnTimer==0) { steinSpawnTimer = steinSpawn; addObject(new Stein(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); } } public void zufälligMuenzenErstellen() { if (--muenzenSpawnTimer==0) { muenzenSpawnTimer = muenzenSpawn; addObject(new Muenze(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); } } }
Super_Hippo Super_Hippo

2017/11/5

#
Remove 'getWorld().' when the code is in a world subclass.
ZigZagNinja ZigZagNinja

2017/11/5

#
ok i did but now it says: cannot find symbol - method showText(int, int, int)
Super_Hippo Super_Hippo

2017/11/5

#
Try to use
""+muenzenSpawnTime
instead of only the variable name.
ZigZagNinja ZigZagNinja

2017/11/5

#
now it says : cannot find symbol - method showText(java.lang.String,int,int) ):
Super_Hippo Super_Hippo

2017/11/5

#
What version of Greenfoot do you use?
ZigZagNinja ZigZagNinja

2017/11/5

#
doesnt matter it works now thanks for your help it really helped me out
Super_Hippo Super_Hippo

2017/11/6

#
Well, it did matter because the method was introduced in version 2.4. So if you used a version before that, this error would have shown up, too.
You need to login to post a reply.