About Monkey 2 › Forums › Monkey 2 Programming Help › Monkey 2 App Methods
This topic contains 5 replies, has 4 voices, and was last updated by
APC
2 years, 3 months ago.
-
AuthorPosts
-
January 12, 2017 at 12:36 pm #6454
Team,
On Monkey X 1 we use these App important methods such as
OnLoading, OnCreate, OnUpdate, OnRender OnSuspend, OnResume and more.
In Monkey 2 I see OnRender and OnUpdate I did not see OnSuspend and OnResume that are important to mobile apps.
Is there a guide in this forum where I can get the info? Does Monkey 2 follow the same structure of MX1 reganding this methods?
For instance I had to call my own OnUpdate from OnRender because it was not called automatically.
Below is the a portion of my code. You can down load it from here http://monkey2.monkey-x.com/forums/topic/monkey-2-sample-app-and-audio-issues/
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273Method OnRender( canvas:Canvas ) OverrideApp.RequestRender()'canvas.Clear(New Color(0.5,0.5,0.5))title_x=canvas.Font.TextWidth(titlemsg) 'get the titlemsg text lengthtitle_x=virtualResolution.X *.5-title_x *.5 'center it on the screenFor Local star:=Eachin starListstar.update()star.draw(canvas)Nextcanvas.Color=New Color(1.0,1.0,1.0)If buttonUP.downcanvas.DrawImage(buttonUP.imgDown,buttonUP.tv.X,buttonUP.tv.Y,0,1,1)canvas.DrawImage(dragonUpAnim[animFrame],x,y,0,1,1)lastAnim=dragonUpAnimelsecanvas.DrawImage(buttonUP.imgIdle,buttonUP.tv.X,buttonUP.tv.Y,0,1,1)canvas.DrawImage(lastAnim[animFrame],x,y,0,1,1)endifIf buttonLEFT.downcanvas.DrawImage(buttonLEFT.imgDown,buttonLEFT.tv.X,buttonLEFT.tv.Y,0,1,1)canvas.DrawImage(dragonLeftAnim[animFrame],x,y,0,1,1)lastAnim=dragonLeftAnimelsecanvas.DrawImage(buttonLEFT.imgIdle,buttonLEFT.tv.X,buttonLEFT.tv.Y,0,1,1)canvas.DrawImage(lastAnim[animFrame],x,y,0,1,1)endifIf buttonRIGHT.downcanvas.DrawImage(buttonRIGHT.imgDown,buttonRIGHT.tv.X,buttonRIGHT.tv.Y,0,1,1)canvas.DrawImage(dragonRightAnim[animFrame],x,y,0,1,1)lastAnim=dragonRightAnimelsecanvas.DrawImage(buttonRIGHT.imgIdle,buttonRIGHT.tv.X,buttonRIGHT.tv.Y,0,1,1)canvas.DrawImage(lastAnim[animFrame],x,y,0,1,1)endifIf buttonDOWN.downcanvas.DrawImage(buttonDOWN.imgDown,buttonDOWN.tv.X,buttonDOWN.tv.Y,0,1,1)canvas.DrawImage(dragonDownAnim[animFrame],x,y,0,1,1)lastAnim=dragonDownAnimelsecanvas.DrawImage(buttonDOWN.imgIdle,buttonDOWN.tv.X,buttonDOWN.tv.Y,0,1,1)canvas.DrawImage(lastAnim[animFrame],x,y,0,1,1)endifcanvas.Color=New Color(1.0,0.0,1.0)canvas.DrawText("x:"+x+" y:"+y,buttonLEFT.tv.X+buttonLEFT.Width,buttonLEFT.tv.Y+buttonLEFT.Height*.5-canvas.Font.Height*.5)canvas.Color=New Color(1.0,0.39,0.0)canvas.DrawText(titlemsg,title_x ,16,0,0)animFrame+=.10If animFrame>=4 Then animFrame=0OnUpdate() ' How can OnUpdate can be invoked automatically?EndMethod OnUpdate()'Reset gamepad keysresetPad()'check for mouse overCheckPadM()'check for touch on the screenCheckPadT()'check for key pressCheckPadK()EndMethod OnMeasure:Vec2i() OverrideReturn virtualResolutionEndJanuary 12, 2017 at 5:07 pm #6462In my (incomplete yet) framework I go away from Window class, user know nothing about window – it hidden inside of my Application class that also don’t override monkey’s App class.
Inside of Application we have needed callbacks.
One way is to use OnUpdate, On… () virtual methods , other way is to use fields-as-func and subscribe to them . I preffer the second way because you shouldn’t extends class to use these callbacks .
OnUpdate – I think it’s normally in monkey2 to call it from render.
OnSuspend and OnResume logic you can extract from OnWindowEvent – see the docs.
January 12, 2017 at 5:43 pm #6463nerobot
Thank you.
In case I am working on a game my game class should extend View and not Window. does that also apply for mobile devices? Or it depends on the type of app I am developing.
January 12, 2017 at 9:49 pm #6471A lot of OnBlah stuff that was in mx1 is now implemented as ‘signals’ in AppInstance, eg: Activated and Deactivated (probably more coming). You can ‘hook’ these up to your own handler like this (inside, say, your window’s constructor):
App.Activated+=OnActivated ‘Assumes there’s an OnActivated method/function somewhere!
App.Deactivated+=OnDeactivated ‘Assumes there’s an OnDeactivated method/function somewhere!For OnUpdate, it’s up to you how to update, easiest way is just to update inside OnRender but you can also create a timer, eg:
New Timer( 60,OnUpdate )
January 15, 2017 at 12:36 am #6512I thought about this one as well as well, at first I thought that it was missing due to some sort of API economy.
For example with “New” you replace the “OnLoad”, with “OnRender” you replace the “OnUpdate”.January 15, 2017 at 12:43 am #6514I posted a template here http://monkey2.monkey-x.com/forums/topic/monkey-2-app-template/ with suggestions from Mark and impixi.
-
AuthorPosts
You must be logged in to reply to this topic.