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


Game Only Works After Closing Greenfoot

moko moko


Hi, I'm working on a sight-reading game where you input the name of the note as it moves across the screen. I'm using a LinkedList to form a Queue that stores all of my notes as nodes, which each contain their answer, index (used to calculate the answer), and note object themselves. When I run my game immediately after opening Greenfoot it works fine, but if I use the reset button or my in-game 'play again' buttons, the answers get messed up and are suddenly of the wrong clef. Basically, my game only works on its first run. I've published the game under 'Musicia' on my profile. There's a lot going on so my source code is all there. Is my problem something to do with previous memory interfering with the correct answers? Thanks in advance!
Spock47 Spock47


This kind of problems often occurs when static attributes are used. (e.g. "TestWorld.notes" should be non-static). A static attribute is bound to the class itself instead of the object. So, if you have two testworlds, the static modifier means that you have only one value for notes that is used by both testworlds. So, if notes is changed by one testworld, the change interferes with the other testworld, too. Especially, if you restart your program and a new testworld object is created, it still continues to use the same (already used) notes value that was used in the first run. In constrast, a non-static attribute belongs to one object - each object of a class has its own value for that attribute. So, to reference it from outside, you need to call it on that object. E.g. if you have an object "world2" which is a TestWorld, a non-static notes attribute can be references by "world2.notes". If you want to call it from an actor that currently is in the world, the getWorld() method will give you the world object; but you will have to cast it to TestWorld, so that the compiler will know that it has a notes attribute (because not every World has a notes attribute), e.g.
So, 1. remove the static modifier from the notes attribute. 2. The compiler will then guide you to the places where notes is called from outside the TestWorld class. 3. Check for other places where you can remove "static", e.g. the method "Queue.isEmpty" would also be easier if it is not static (it then can be simply called by "notes.isEmpty()" instead of "Queue.isEmpty(notes)" or "notes.isEmpty(notes)"). Live long and prosper, Spock47
You need to login to post a reply.