Mojo Null Nightmare! :(
Monkey Forums/Monkey Beginners/Mojo Null Nightmare! :(
| ||
Hey folks, I'm a Monkey newbie and so far things have been going really well. However, I've hit a massive block and it's totally stumped any further progress. Here is the error in all browsers: Monkey Runtime Error : Null object access /Applications/MonkeyX77a/modules/mojo/graphics.monkey<450> /Users/nclayden/Desktop/game/gameTest.monkey<32> /Applications/MonkeyX77a/modules/mojo/app.monkey<65> Here is my set-up/file structure - OSX 10.9.1 running Safari: /Users/nclayden/Desktop/game/ gameTest.data mage1.png gameTest.monkey Here is the code: [CODE] Strict Import mojo Class MyGame Extends App Field player:Image Field FPS:Int = 60 Method onCreate:Int() SetUpdateRate FPS player = LoadImage("mage1.png") If player = Null Print ("ERROR: Image not found") Endif Return 0 End Method OnUpdate:Int() Return 0 End Method OnRender:Int() Cls (156,189,15) DrawImage (player, 10, 10, 0) Print ("Game Loaded") Return 0 End End Function Main:Int() New MyGame Return 0 End [/CODE] Can anyone please help untangle my brain and point me to what I've done wrong? |
| ||
do you have the image in the gameTest.data folder? |
| ||
Hi Supertino, I do, not clear in the description above but it's in there. Correct path is the following: /Users/nclayden/Desktop/game/gameTest.data/mage1.png |
| ||
[edit] I was wrong. better replies bellow. |
| ||
I should note that loading images in the main update loop is not a good idea, possibly have a method called LoadImages() and then make sure you call it only once at runtime, I would also think about looking into diddy once you have the bascis of monkey down, it will help you out a lot in terms of handling all your sprites, and audio. hope this helps. |
| ||
Probably the game is trying to draw the image before it is finished loading. try testing if player isn't null on the drawimage player line, and it should work. |
| ||
I see the problem, your method is spelt onCreate and should be OnCreate |
| ||
Supertino beat me to it! You made a new function onCreate() that never gets called. |
| ||
DOH! I say again.. DOH! Dude, thank you so much. I must have been staring at that for an hour or so. Boy do I feel like a fool.. :P Me thinks some greater attention to detail is required in future. |
| ||
Ha I totally missed that. |
| ||
Paul, with all respect but it is totally fine to load some resources in OnCreate. Espercially just a few images. The fields are created with the instance of the corresponding class. |
| ||
I would.kill fot a required "overloads" directive |
| ||
Hey Clutch, We've all been there, it's almost a right of passage :) Welcome to Monkey! |
| ||
@Paul - When you call OnCreate it's creating the instance of the game, you are trying to load your image into a field that's not actually been created yet. Sorry but this is incorrect, as Mike has already stated it is fine to load resources in OnCreate... |
| ||
Yeah your right of course, not sure what I was thinking, I posted to fast should have taken a second to actually look at the code and understand the issue before sending a reply. thanks for pointing that out would hate for anyone else to read that and get the wrong idea because I was to lazy to produce a proper response :/ |
| ||
An overloads directive has its points (I saw discussion of something similar in Java but I don't think it is implemented). But I am not crazy about it being required, it would seem to make many patterns rather long-winded. I wouldn't mind if you could add an optional Overload keyword that would give an error if it's not actually overloading a super-class method or function (and for symmetry a Not Overload keyword that would do the opposite). |