JavaScript is not Java
Limits
How To Do It
Problem Solving
Debugging JS
Converting Your Java Game to JavaScript -- What you need to know (You are here)
Your Own Java Game -- Step by step tutorial to build a simple "Pong" game
The Itty Bitty GameEngine -- Overview
Class GameWgt -- The visual components of a GameEngine game
Overriding GameEvent -- The programmatic components of a GameEngine game
The Pong example game as JavaScript: www.IttyBittyComputers.com/Java/GE.php?name=Pong
Java and JS are both Turing-complete, so in principle you can write
a translator from one to the other, but translating an untyped language
like JS into a fairly strongly typed language like Java is awkward. I know,
I wrote a compiler (the same idea) for HyperTalk, which was also untyped.
The other direction is easier, and numerous Java->JS translators exist.
So why don't we use one of those? The simple answer (I like simple) is
that I can (and did) write a much smaller translator that runs as a part
of GameEngine that converts a limited subset of Java to something really
small that interfaces directly to a carefully tuned JS version of the GameEngine.
This is a game we are talking about, so it needs to be done carefully or
it will be too slow to play on your average browser. Chomp was designed
with that in mind.
The translator and the JS GameEngine both know about widgets (and you can use them) but the JS version of your game cannot create any new widgets. If you need to add widgets during the game, you should create them all in GameMaker, then hide them until needed. Also text fields cannot get bigger than what GameMaker made them. Again, make it big enough for the largest possible text, then use what you need.
There are some widget and event methods that are not supported, so if their names show up anywhere in your Java code, the translator will refuse it. The translator is not very smart, it only looks for character strings, so don't even use these names as part of larger names:
GameEnding GetTextWide SwapData SetFocus CheckIt RadioBtn FindElemt DeleElemt XchElemt AddElemt KeyData
If you are testing a widget reference against null, you need to
do it this way:
if (myWgt==null) whatever();and not:
if (null==myWgt) whatever();
When declaring your own methods, you need to put the return type
on the same line as the name and the left parenthesis, but the parameters
can spill over onto up to eight more lines.
Java allows you to declare the control variable inside the parentheses of a for-loop; JS probably does too, but variable declarations are different, and my translator only looks for declaration type names at the front of a line or inside function parameter lists. Move your control variable declaration out and into a normal variable declaration line.
When you use block-comments, the translator assumes they span more than one line, like JavaDoc comments at the front of each method. Don't confuse it. Anyway, the comments are removed. Publish your original Java if you want people to read your comments.
Alert and input dialogs are not supported at this time. Text input in general is not yet supported. I ran out of time. Work-Around: use DefaultKeyFocus for a widget to accept keys, then enter the text into your fields in your own code.
Changing the size on a Ball widget is not yet supported. You can try,
but nothing will happen. Work-Around: there are only four defined sizes
at this time, so just make one each size and hide the sizes you are not
using.
After your game is running in Java, do one more Build (you may
need to make a trivial change, like turning a checkbox on then back off).
If the button becomes "NoJS", something went wrong and you can click
on it for an error message (see "Problem Solving"
below). Otherwise it will become a "->JS" button which you can click
on. It will then ask for a "username_password" which you must use the name
you created in Chomp (see "Remembering
What You Told the Computer to Do"; I ran out
of time; maybe a future revision will let you create an account here).
When it tells you "OK" with a URL, your game
has been uploaded. In general the URL is the same
as the Pong link at the top of this page, but with your username in place
of the name "Pong". If it doesn't say OK, then something went wrong
and you should have a new "NoJS" button to click for the Uploader
error message. Bad Things Happen...
These are the error messages you might get from the translator program:
Can't find class name -- When you first Build your game program it creates a Java class file named with your game name. If you alter the class name in that file without changing the file name and game name to match, or if you alter the spacing of the words on the class declaration line, then you could get this error. Solution: recreate your Java file by moving this file to another directory and rebuild your game, then copy your own Java code from the old file into the new file.Can't find widget list -- The widget list is a string of numbers and other stuff generated when you click the Build button. It will probably fail if you alter the meta-tags that are used to find the widget list, so this error is unlikely (the build will fail instead, and you probably need to recreate your Java file.
GameList front corrupted
GameList back corrupted -- The Java version of the widget list is replaced by a pre-constructed array of numbers, which changes the form of the GameList method. If you made substantial changes to it you might get one of these errors.Can't do this method -- The following list of GameEvent and GameWgt methods are not supported in this version of the translator, so you should not even mention them (nor anything containing the same substring) in your game code:
GameEnding GetTextWide SetBlinker DrawMe IsValidIDtxt GetIDtxt SwapData FixHaff HexyMore FmtInfo FmtFront LogMe SetFocus CheckIt RadioBtn FindElemt DeleElemt XchElemt AddElemt toStrMash
Invalid GameWgt object in method call -- I never do this, but I think Java lets you use the object returned from a method call directly to access a method in that object's class without storing the reference in a variable. Java may accept it but my translator won't. Make sure your calls to widget methods are of the form "objname.methname(..." with no spaces between the period and the parenthesis.Unrecognized op after 'null'
Unrecognized op before 'null' -- Because your translated game program running with the JS version of GameEngine has no use for objects in JS, the legitimate Java object references are converted into variables that are not objects in JS. Part of this conversion involves converting the keyword "null" to 0, but the translator tries to verify that it is legitimate. Basically if you are using references to GameEngine objects (one of the three public classes), you can assign them to null or test them against null and it should work, but only if the "null" is on the right of the comparison operator:wgtref = null; // OK
if (wgtref==null) DoSomething(); // OK
if (wgtref != null) DoSomething(); // OK
methcall(null,wgtref==null); // OK
if (null==wgtref) DoSomething(); // will fail
Generated text too short -- This probably can't happen with a credible game.Invalid wgt list end
Failed wgt list conversion
Bad widget list form
Invalid line number in widget list
Widget spec line is too short
Invalid negative data length -- The Build process constructs a widget list in the correct form, so you might have difficulty causing one of these errors to happen.Macros and private widget types not supported -- If you only use the defined widget tools in GameMaker, and if you leave the "Alert+InputDlog" checkbox unchecked, you should not get this error.
No such font -- The GameEngine has seven built-in fonts. I tried to sneak in some other font number, but the input replaced it with one of the built-ins. But if you succeed, you might get this error.
The uploader has its own set of error conditions. You might get
a message like this, which I don't know what all the numbers mean, but
it's basically a transmission error, and you can try again later or ask
one of us for help:
Uploader error #nn
HTTP error code = nnn
Or you might get one of these messages, which come from the server:
Notice: User not found -- which means your name or password failed. Go back to Chomp and verify that you have a valid Login.Notice: insufficient permission to save
Error: missing data parameter
Error seeking user name
Error: Could not handle save command -- These are all problems inside the Server, not your fault. Let one of us know and we'll try to fix it.
Sometimes JS scripts won't even load, and the only way to figure out
what went wrong is to run the script through a "JavaScript validator" which
you can find several freebies online (I used this
one to debug Chomp). Some of these sites will look at your web page,
some want you to you paste your script into a box. If the translator succeeded,
then it created a ".js" file which you can use for validation.
If the validator gives you more than one error, you can only trust the
first. If you can figure it out, great. If not, ask one of us. Maybe we
can't figure it out either, but we have more experience finding problems.
Don't be ashamed, this is how you learn. JavaScript is a crummy language
to learn on, but you never know when you might need it.
Tom Pittman
Rev. 2020 August 1