Simple Button Class
Monkey Forums/Monkey Code/Simple Button Class
| ||
Here are a couple of simple classes for control buttons. These expect a global int "touches." Every tick on update, the multitouch interface should be polled like so: touches=0 For Local i#=0 Until 32 If TouchDown( i ) touches+=1 Next Here are the classes for tapregion and holdregion: Class tapregion ' this is a region that can be tapped or held down. This is for something where you don't ' want the player to have to keep hitting the button in order to get the result-- he can tap it ' or hold it. Field ux#,uy#,lx#,ly# Method tapdown:int() Local tapx#, tapy#, i#, touchedme# For i = 0 To touches If inrect(TouchX(i),TouchY(i), ux, uy, lx-ux, ly-uy) touchedme = 1 Next Return touchedme End Method End Class Class holdregion Extends tapregion ' this is a region that only returns 1 if it is being clicked for the first time in a row, ' eg. you can click it once and hold it down and it will stay down but not continue firing. ' this is for something like a jump button where you want the player to have to continue hitting ' the button in order to jump more than once. Field olddown# ' was it touched last tick? Method tapdown:int() Local tapx#, tapy#, i#, touchedme# For i = 0 To touches If inrect(TouchX(i),TouchY(i), ux, uy, lx-ux, ly-uy) touchedme = 1 Endif Next ' Should only return 1 if touchedme = 1 and olddown = 0 ' There is probably some more concise way to do this but concise isn't always good If touchedme = 1 And olddown = 0 olddown = 1 Return touchedme Else If touchedme = 0 olddown = 0 Return 0 endif End Method End class Regions (or buttons) should be defined thusly: Global leftbutton:tapregion = New tapregion Global rightbutton:tapregion = New tapregion Global jumpbutton:holdregion = New holdregion Then you set the size of the buttons like so: jumpbutton.ux = 0 jumpbutton.uy = 0 jumpbutton.lx = DeviceWidth() jumpbutton.ly = DeviceHeight()/2 leftbutton.ux = 0 leftbutton.uy = DeviceHeight()/2 leftbutton.lx = DeviceWidth()/2 leftbutton.ly = DeviceHeight() rightbutton.ux = DeviceWidth()/2 rightbutton.uy = DeviceHeight()/2 rightbutton.lx = DeviceWidth() rightbutton.ly = DeviceHeight() This is for an arrangement where the jump button is the upper half of the screen and the left and right buttons are the left and right half of the lower half of the screen. When you want to poll the button in your player input code, just do if jumpbutton.tapdown()) Or whatever you want. You could easily have regions that counted up the number of fingers that were in them or something like that. Or had images and a draw method. |