[SOLVED] Monkey Runtime Error : App...
Monkey Forums/Monkey Beginners/[SOLVED] Monkey Runtime Error : App...
| ||
Hello, first of all, I'm sorry if this question has already been answered somewhere else or if it is the wrong place for my topic. I made a very simple programm with two class starting like this : "Class Affichage Extends App" "Class Bouton Extends App" But if I run my program I have this error : Monkey Runtime Error : App has already been created C:/MonkeyX77a/modules/mojo/app.monkey<104> C:/Dropbox/Programmes/Test.monkey<15> C:/Dropbox/Programmes/Test.monkey<9> If I run my programm with only one class, it works perfectly. But when both are activated, I have this error message. I don't see what's wrong... I guess it has a link with "Extends App" but I don't know how to solve this. |
| ||
Why are you extending App in both classes? Can you provide a complete code sample for us to help you? |
| ||
Yes you are correct the issue is with "Extends App", for a Mojo application you should only have one class extending App. Normally something like this: Strict Import mojo Function Main:Int() new MyGame() Return 0 End Class MyGame Extends App Method OnCreate:Int() Return 0 End Method OnUpdate:Int() Return 0 End Method OnRender:Int() Return 0 End End |
| ||
The App class is special, it sets up mojo and is intended as the unique entry point for a mojo program. So typically there's a Main() function (all Monkedy programs start with this) that constructs a single App derived class. You then set up your game loop by overriding App.OnCreate(), App.OnUpdate(), App.OnRender(), and maybe some others. If what you want to do is have button classes etc. have access to app data and functions, the simplest solution is to declare a global App-derived object, for example: Then your button classes will be able to get at any information held in theApp. |
| ||
Ok thank you for such a quick answer ! I wanted to have : - a class "button" to create many buttons - a class "counter" to display scores, how much wood I have, how much gold etc.... These class need to do some stuff onCreate, onUpdate and onRender. I tried a new version of it, that do not give me any error message, but nothing appears anymore. I went through the tutorial, but all the examples are given with only one class (Extends App), not two like I do. This is my old code. Sorry for the french comment, but I think that with the above explanation it should be easy to understand without them. Import mojo Function Main () Local start:Bouton = New Bouton start.x = 150 start.y= 150 Local score:Affichage = New Affichage score.x = 300 score.y = 300 End Class Affichage Extends App Field x:Int 'Emplacement selon x Field y:Int 'Emplacement selon y Field largeur:Int = 64'Largeur du carré Field hauteur:Int = 32'Hauteur du carré Field text:String = "Score" Method OnCreate () SetUpdateRate 60 End Method OnUpdate () Local hit=KeyHit( KEY_LMB ) 'Uses KeyHit to check the left mouse button. If MouseY()>y And MouseY()<(y+hauteur) And MouseX()>x And MouseX()<(x+largeur) Print "survol du score, afficher des infos" End End Method OnRender () Cls 64, 96, 128 SetColor ( 255, 255,0 ) DrawRect x, y, largeur, hauteur DrawText text,x+largeur/2,y+hauteur/2,0.5,0.5 End End 'Button creation Class Bouton Extends App Field x:Int 'Emplacement selon x Field y:Int 'Emplacement selon y Field largeur:Int = 64'Largeur du bouton Field hauteur:Int = 32'Hauteur du bouton Field text:String = "Bouton" Method OnCreate () SetUpdateRate 60 End Method OnUpdate () Local hit=KeyHit( KEY_LMB ) 'Uses KeyHit to check the left mouse button. You could also use MouseHit( MOUSE_LEFT ) If hit And MouseY()>y And MouseY()<(y+hauteur) And MouseX()>x And MouseX()<(x+largeur) Print "clic sur le bouton" End End Method OnRender () Cls 64, 96, 128 SetColor ( 255, 255,0 ) DrawRect x, y, largeur, hauteur DrawText text,x+largeur/2,y+hauteur/2,0.5,0.5 End End And this is the new version I tried. No more error message. It runs but nothing appear... Import mojo Global theApp:MyApp Function Main () theApp = New MyApp() Local start:Bouton = New Bouton start.x = 150 start.y= 150 Local score:Affichage = New Affichage score.x = 300 score.y = 300 End Class MyApp Extends App End Class Affichage Field x:Int 'Emplacement selon x Field y:Int 'Emplacement selon y Field largeur:Int = 64'Largeur du carré Field hauteur:Int = 32'Hauteur du carré Field text:String = "Score" Method OnCreate () SetUpdateRate 60 End Method OnUpdate () 'The mouse is over the area If MouseY()>y And MouseY()<(y+hauteur) And MouseX()>x And MouseX()<(x+largeur) Print "survol du score, afficher des infos" End End Method OnRender () Cls 64, 96, 128 SetColor ( 255, 255,0 ) DrawRect x, y, largeur, hauteur DrawText text,x+largeur/2,y+hauteur/2,0.5,0.5 End End Class Bouton Field x:Int 'Emplacement selon x Field y:Int 'Emplacement selon y Field largeur:Int = 64'Largeur du bouton Field hauteur:Int = 32'Hauteur du bouton Field text:String = "Bouton" Method OnCreate () SetUpdateRate 60 End Method OnUpdate () 'On clic on the area Local hit=KeyHit( KEY_LMB ) 'Uses KeyHit to check the left mouse button. If hit And MouseY()>y And MouseY()<(y+hauteur) And MouseX()>x And MouseX()<(x+largeur) Print "clic sur le bouton" End End Method OnRender () Cls 64, 96, 128 SetColor ( 255, 255,0 ) DrawRect x, y, largeur, hauteur DrawText text,x+largeur/2,y+hauteur/2,0.5,0.5 End End |
| ||
The "On..." Methods are only automatically called for the App. You need to call them yourself like this: |
| ||
Ok, I understand the logic now, thank you ! I had to delete the cls instruction to see the result, but my problem is solved now ! Thanks again. |