My basic program
Monkey Forums/Monkey Programming/My basic program
| ||
Hello all. Before I go any further with my world beating app ;) could someone have a look at this basic code and tell me if I have got the general idea with the way monkey works? I'm not sure if I have declared the variables in the right place but it's the only place I can get them to work. I know that OnRender only runs at the current UpdateRate but does the OnUpdtate method run at the update rate set in OnCreate too? If so, does that mean I can only poll the user input at the current update rate? Also, is it possible to have different update rates for the OnUpdate and OnCreate? Strict Import mojo Function Main:Int() Local app:testApp app = New testApp Return 0 End Function Class testApp Extends App Field BGimage:Image Global noise:Sound Global hit:Int Method OnCreate:Int() SetUpdateRate 60 noise = LoadSound("sound.wav") BGimage=LoadImage("background.png") Return 0 End Method Method OnUpdate:Int() hit = TouchHit(0) If hit = 1 PlaySound (noise,0,0) hit = 0 Endif Return 0 End Method Method OnRender:Int() DrawImage BGimage,0,0 Return 0 End Method End Class I was intending to make a sound board just to get used to how things work, but due to the latency between touching the screen and playing the sound (if compiled to android, no lag in HTML5), I shelved the idea! Most sound boards are the same, although I have had a couple of piano type apps which demonstrate a lot less latency. Sorry if this is still all a bit noob, I'm getting there... slowly... Thanks, Dan. |
| ||
I dont think there are any issues in your code and there should be no lag between touching and playing sound. What device are you on? Or is this the emulator? The emulator tends to be quite slow compared to real devices. I have made a few soundboards with no issues BTW : 60fps is not needed for a soundboard ;-) |
| ||
Hi pantson, thanks for the reply. I am compiling to HTML5 for quick testing but compiling to an actual Galaxy S2 on 4.0.3 as well. (edit: also to an Xperia X10 on 2.3.3) I believe the 60fps does contribute to the lag a little and it can also make the program 'miss' a click. If I set it to 1fps, there isn't enough time to register the click / hit (HTML5 / Android). Even at 10fps the program hardly registers a hit. At 60fps the hit is sometimes not registered too, at 1000fps the click registers almost every hit you make. The soundboard I was trying to make was to play sounds along in time to music, not necessarily just drums but if you think of it like that, thats the general idea. When you hit the screen / click the mouse, the sound needs to be instant, I even chopped the sounds right down at the start of the sample (in an editor) to make them even more immediate. The sounds play acceptably in HTML5 but the lag in Android (tested on 4.0.3 and 2.3.3) makes the app unusable for what I intended. As a basic soundboard, I agree, it would be fine. Dan. |
| ||
Strict Import mojo Function Main:Int() New testApp Return 0 End Function Class testApp Extends App Field BGimage : Image Field noise : Sound Method OnCreate:Int() SetUpdateRate( 60 ) Self.noise = LoadSound("sound.wav") Self.BGimage = LoadImage("background.png") Return 0 End Method Method OnUpdate:Int() If TouchHit() PlaySound( Self.noise ) Endif Return 0 End Method Method OnRender:Int() Cls 255, 255, 255 DrawImage BGimage,0,0 Return 0 End Method End Class |
| ||
Thanks NoOdle, that tidied it up a bit, although I don't understand fully how the Self.x works and why it benefits me (sorry). You also removed the hit variable, I can see why, but did I have it declared properly in my original code, as Global? (Just in case I do ever need any more variables). I am still struggling to understand the OnUpdate OnRender methods too. If I wanted to make some maths code run very fast (as fast as the device can do it) but only update the graphics 60 times per second, where do I put the maths code? Cheers muchly, Dan. |
| ||
The problem is with the:hit = TouchHit(0) If hit = 1 This function can return a number greater than 1 if there have been more than one click during the frame loop (that's why lower frame rates do behave worse). Suggestion: hit = TouchHit(0) If hit > 0 Writing Self is optional when you're refering a member to a class, but it is recommended as it can make the code clear. I would suggest you to use Self when you refer to a non shared member of a class, from inside the class. That is, when your refering to a Field or a Method. And I would suggest you to use the class name when you're refering to the shared members of a class, that is Const, Globals and Functions. My recomendation in order to make code clear and easier to maintain: Class TestClass Field myField:Int; Const MYCONST:Int = 1024 Global AGlobalVar:String = "" Method DoSomething:Int(value1:String) 'Example code: Local myVariable:Int = self.myField + TestClass.MYCONST 'See how members are referenced here Local myString:String myString= TestClass.EmptyFunction(myVariable) AGlobalVar = "The result is " + myString End Function EmptyFunction:String(parameter1:int) 'We do nothing here. End End |
| ||
FYI: Android does have low latency issues with sound: http://www.androidannoyances.com/post/tag/low-latency-audio http://www.rossbencina.com/code/dave-sparks-on-android-audio-latency-at-google-io-2011 |
| ||
Good find therevills. Interesting reading. I think I'll ditch the audio tools for now then! Sorry for asking again but... ...If I wanted to make some maths code run very fast (as fast as the device can do it) but only update the graphics 60 times per second, where do I put the maths code? Is it a question of calling a new method with the maths code in it from inside OnUpdate... then OnUpdate continues to the next line while the new method called is just off and running on it's own, as fast as it can, not connected in any way to the OnUpdate anymore? Edit: I think what I am asking is how do you make some code run independently of the game loop? Outside the limitation of UpdateRate? Dan. |
| ||
"Writing Self is optional when you're refering a member to a class, but it is recommended as it can make the code clear." Do a lot of people recommend this? I would consider it very long-winded, and indeed I try to avoid ever having to use Self. I vaguely recall some people talking about using this-> in a similar way in C++, but I am pretty sure it was very much a minority position. |
| ||
I use Self in my code, its handy for big projects as I can quickly identify what is what. |