Initializing Seaman for GameMaker


<<Previous | ToC | Next >>

At this point in your development of Seaman you should have finished laying out your widgets in GameMaker. Your picture does not need to look like mine, but I will use mine for reference, and you may need to make adjustments to accommodate your differences. Only the stick figure widgets and the dash/letter text items will be referenced in your code -- unless you want your boat to sail away when the seaman is fully on board, but we'll leave that for after the game is working.

In GameMaker, click the Build button, then quit (close the window). In Replit the Seaman.java file that GameMaker made should already be in your Repl. You probably did something like this if you made another GameEngine program.

Your initialization goes in two places in the Seaman class window. Any new class-wide variables you declare will go immediately before the first "$" line, or (as I prefer) immediately after the second "$" line. Any other initialization code goes inside the Startup() method, preferably after its second "$" line but before the closing brace.

The seaman's body parts need to be hidden as part of the initialization. If you gave them Java-compatible names they are already declared for you, all you need to do is (in the Startup() method) hide the eight (or however many) widgets you have:

if (LefLeg != null) LefLeg.HideSho(false); // hide this body part
if (RitLeg != null) RitLeg.HideSho(false); // ..and so on


The text dashes widgets could be hidden the same way, but later code is much messier if you don't put them into an array that can be indexed in a for-loop, so we do that as part of the initialization. First you need to declare your persistent arrays, the word to be guessed as an array of characters at the top of the class, after the second "$" line:

static char[] guessword = new char[32];
and an array of widgets:
static GameWgt[] dashes = new GameWgt[32];
The "static" shouldn't be necessary, but I found that (at least in the version of BlueJ I was using) if you don't make these arrays static, they get deleted between events. This kind of bug is typical in unix programmers (see "Flat vs the Real World") because they have little or no experience doing event-driven software. They run the world, so get used to it.

From the initialization part of our English program design, we see three other variables that need to persist across events, so we declare them here too:

boolean playing = false;
int nLetts = 0, wrong = 0;

The English initialization code does not say much about how to "preset the array ... dashes to be underscore characters '_' " but it is, as we sometimes say ironically, "a simple matter of programming." For one thing, the array is widgets, not characters. The widgets are already declared with their own reference variables, we need to get those same references into our array. If you defined them as I did, with the same name differing only in the sequence number, then we can construct the names on the fly in our for-loop, inside the Startup() method:
for (int nx=0; nx<32; nx++) {
GameWgt wgt = myGame.FindListWgt("{DashLet" + nx + "}");
if (wgt != null) {
int zx = wgt.PutTextLn("_"); // set it to dash
wgt.HideSho(false);}        // then hide it
dashes[nx] = wgt;} // end of for-loop
Of course I used my variable names, you need to use your own, whatever names you used.

In the next page we'll build the Java code to play the game.

<<Previous | ToC | Next >>

[2022 November 8]