How do I make my counter, count up slower?
Monkey Forums/Monkey Beginners/How do I make my counter, count up slower?
| ||
The code that I have below is a counter. Every time the "up" arrow key is pressed, the timer goes up. But, the timer goes up too fast. I want it to go up ONE number every time the up arrow key is pressed (for example if the up arrow key is pressed once the counter would show 1, if the up arrow key is pressed again, the counter would show 2, and so forth). I hope this makes sense. Strict Import mojo Function Main:Int() New MyApp() Return True End Class MyApp Extends App Field Count: Int Method OnCreate:Int() SetUpdateRate(60) Return True End Method OnUpdate:Int() If KeyDown(KEY_UP) Count+=1 End Return True End Method OnRender:Int() Cls DrawText "Count :" + Count, 10, 10 Return True End End |
| ||
In simple terms you want to add a pause before the the key is detected again. Note using Millisecs() does have a few caveats.Strict Import mojo Function Main:Int() New MyApp() Return True End Class MyApp Extends App Field Count: Int Field last_ticks:Int Method OnCreate:Int() SetUpdateRate(60) Return True End Method OnUpdate:Int() Local ticks:int=Millisecs() If KeyDown(KEY_UP) And ticks > last_ticks+1000 Count+=1 last_ticks = ticks End Return True End Method OnRender:Int() Cls DrawText "Count :" + Count, 10, 10 Return True End End Or use KeyHit |
| ||
Oops, I'm sorry you all. I figured it out. I changed the "KeyDown(Key_Up)" part to "KeyHit(Key_Up)" as in the code below. Thus, I answered my own question. I couldn't figure it out at first because of the layout of the actual code I was writing (i.e. above is just a tiny fraction of the actually code I wrote with the problem in there). Anyway, the answer to my question and how I solved it is in the code below in case someone asks this question again and to help the next person, or in case anyone wants to know what I did: I wrote it in such a way that one can just copy and paste it into the IDE! Thanks!Strict Import mojo Function Main:Int() New MyApp() Return True End Class MyApp Extends App Field Count: Int Method OnCreate:Int() SetUpdateRate(60) Return True End Method OnUpdate:Int() 'Here is what I changed. I changed "KeyDown" to "KeyHit" and 'it worked the way I wanted it to; aka, the 'counter now counts up slowly in the one by one (1,2,3,4, etc) 'sequence that I wanted. Thanks! If KeyHit(KEY_UP) Count+=1 End Return True End Method OnRender:Int() Cls DrawText "Count :" + Count, 10, 10 Return True End End |
| ||
Thanks dawlane. What you wrote above was the other way I was going to ask about. Thanks again |