Errors when following tutorial
Monkey Forums/Monkey Beginners/Errors when following tutorial
| ||
I was watching and following the video tutorials referenced here: [url]http://www.monkey-x.com/Community/posts.php?topic=3318&page=1[/url] and have run into a snag. When i try to run my code i get the error: Main.monkey<26> : Error : Syntax error - expecting identifier. My code is very identical to the one presented in the tutorial with a few minor differences. Here is the code I'm using: ' import required libraries Import mojo ' constants Const STATE_MENU:Int = 0 Const STATE_GAME:Int = 1 Const STATE_GAMEOVER:Int = 2 Class VectrixQuest Extends App ' holds games current state Field gameState:Int = STATE_MENU Method onCreate() 'set update rate SetUpdateRate(30) End Method onUpdate() ' perform actions based on game state Select gameState ' menu screen state Case STATE_MENU ' draw the title text DrawText("Vectrix Quest", 320. 240, 0.5) ' game state Case STATE_GAME ' game over state Case STATE_DEATH End End Method onRender() ' clear the screen cls(0,0,0) ' perform actions based on game state Select gameState ' menu screen state Case STATE_MENU ' game state Case STATE_GAME ' game over state Case STATE_DEATH End End End Function Main() New VectrixQuest() End Any assistance in this matter would be greatly appreciated. Sincerely, Vectrix Quest |
| ||
You made a small typo: point/dot instead comma. Change DrawText("Vectrix Quest", 320. 240, 0.5) to DrawText("Vectrix Quest", 320, 240, 0.5) |
| ||
Also, Monkey is a case sensitive language, so your method names should be OnCreate, OnUpdate, OnRender, etc. You should do your drawing in the OnRender method, so you should move your DrawText line of code from OnUpdate into OnRender. Hope that helps. |
| ||
Interesting. Monkey does not catch the other errors if it does not include the methods. After changing the method names to OnCreate, OnUpdate, and OnRender, more errors come up: cls() -> Cls(), STATE_DEATH -> STATE_GAMEOVER Changed code: |
| ||
it's because the methods never get called. And if they don't get called, they get removed from the program. It is as if the method and code don't exist. It's due to Monkey's aggressive dead code elimination. |
| ||
Make it a habit to start your code with the strict keyword. Also using reflection helps finding bad code in unused code sections. |
| ||
it's because the methods never get called. And if they don't get called, they get removed from the program. It is as if the method and code don't exist. It's due to Monkey's aggressive dead code elimination. I expected the optimization (dead code elimination) to come after the syntax check. No problem, just good to know. ;) Strict Import mojo Class Game Extends App Method NeverCalled:Void() this gets never called so no syntax check End Method OnCreate:Int() SetUpdateRate(30) Return 0 End Method OnRender:Int() Cls(64,64,64) Return 0 End End Function Main:Int() New Game() Return 0 End |