Null object for Object Timer?
Monkey Forums/Monkey Beginners/Null object for Object Timer?
| ||
Hi all, I'm new to Monkey X and the forums. I have a Fantom Engine question. I hope it's okay to ask it here. I want to use CreateObjTmer just for the timer. I don't need OnUpdateTimer to do anything to an object but the parameters for the overload are ftObject, TimerId. So I created a generic object and assigned it Null. It still doesn't work. Do you have to use a non-null object for CreateObjTimer? OnObjectUpdate Method ... CreateObjTimer(g.generic, g.tmShotTime, 3000) 'OnObjectTimer Method Method OnObjectTimer:Int(timerId:Int, obj:ftObject) 'check for shot Time timer If timerId = g.tmShotTime Then 'timer is done so allow player to shoot again g.playerCanShoot = True 'reset bulletCount (give player 3 more shots before timer fires again) g.bulletCount = 0 Endif Is it not possible to use Null objects here? Thanks. |
| ||
You might be better off posting this in the FantomEngine thread as it is not obvious from your title that this is related to FantomEngine, so Mike might not see it. Try here: http://www.monkeycoder.co.nz/Community/posts.php?topic=5315 |
| ||
Hi underdummer, next time just prefix your topic title with fantomEngine. Makes it easier to find. Or ask at my own forum. Anyway, ftEngine.CreateTimer is what you might need. A null object won't work as there is a reference between an object and a timer when you create the timer like you had. And the object needs to be updated for the object timer to update. So use use ftEngine.CreateTimer. That should work. |
| ||
Hi MikeHart, Thank you for the response . I will use fantomEngine in my subject next time I post on Monkey X. Should I be asking fantomEngine questions on this website forum or the fantomGL support forum? Anyways, I replaced my CreateObjTimer with CreateTimer but it's not working. When using CreateTimer, is the timer still updated in the OnObjectTimer() Method? I did not get a compile error but my app acts as if the timer never triggered. Thanks. |
| ||
I will post a sample over the day. |
| ||
Here you go:Strict #rem Script: Timer.monkey Description: Sample script to show how to use timer Author: Michael Hartlef Version: 1.0 #End ' Set the AutoSuspend functionality to TRUE so OnResume/OnSuspend are called #MOJO_AUTO_SUSPEND_ENABLED=True ' Import the fantomEngine framework which imports mojo itself Import fantomEngine ' The _g variable holds an instance to the cGame class Global _g:cGame '*************************************** ' The cGame class controls the app Class cGame Extends App ' Create a field to store the instance of the cEngine class, which is an instance ' of the ftEngine class itself Field fE:cEngine ' Define two constants for timer IDs Const engineTimer:Int = 1 Const objectTimer:Int = 2 '------------------------------------------ Method OnCreate:Int() ' Set the update rate of Mojo's OnUpdate events to be determined by the devices refresh rate. SetUpdateRate(0) ' Create an instance of the fantomEngine, which was created via the cEngine class fE = New cEngine ' Create a timer without a connection to an object which creates new circles at every second fE.CreateTimer(engineTimer, 1000, -1) Return 0 End '------------------------------------------ Method OnUpdate:Int() ' If the CLOSE key was hit, exit the app ... needed for GLFW and Android I think. If KeyHit( KEY_CLOSE ) Then fE.ExitApp() ' Determine the delta time and the update factor for the engine Local timeDelta:Float = Float(fE.CalcDeltaTime())/60.0 ' Update all objects of the engine If fE.GetPaused() = False Then fE.Update(timeDelta) Endif Return 0 End '------------------------------------------ Method OnRender:Int() ' Check if the engine is not paused If fE.GetPaused() = False Then ' Clear the screen Cls 255,0,0 ' Render all visible objects of the engine fE.Render() Endif Return 0 End End '*************************************** ' The cEngine class extends the ftEngine class to override the On... methods Class cEngine Extends ftEngine '------------------------------------------ Method OnObjectTimer:Int(timerId:Int, obj:ftObject) ' This method is called when an objects' timer was being fired. If timerId = _g.objectTimer obj.Remove() Endif Return 0 End '------------------------------------------ Method OnTimer:Int(timerId:Int) ' This method is called when an engine timer was being fired. If timerId = _g.engineTimer ' Create a simple circle Local circle := self.CreateCircle(20,Rnd(self.GetCanvasWidth()),Rnd(self.GetCanvasHeight())) ' Two ways to create an object bound timer ' circle.CreateTimer(_g.objectTimer, 1500) self.CreateObjTimer(circle, _g.objectTimer, 1500) Endif Return 0 End End '*************************************** Function Main:Int() ' Create an instance of the cGame class and store it inside the global var 'g' _g = New cGame Return 0 End |
| ||
Thank you MikeHart. I can now use CreateTimer like a pro! |
| ||
:-) |