How to create a button
Blitz3D Forums/Blitz3D Beginners Area/How to create a button
| ||
Hello all, i want to create my first game with blitz3D. But i have a problem in the main menu. How do you create a button,and when you click on it then you go to next menu? who can help me?? |
| ||
Basically you create an image as button, and then check if the mouse is inside the image or collide with it when you click a mouse button. You can check for a collision between the mouse pointer image and the button image (ImageCollides command); or check if the mouse pointer collides with a rectangle (ImageRectCollide command). Check also ImageRectOverlap and ImageRectCollide commands. Example: Graphics3D 800,600,0,2 ;button properties bx = 300 by = 300 bw = 128 bh = 64 While Not KeyDown(1) Cls ;draw a filled rect Color 0,200,0 Rect bx,by,bw,bh,True ;draw text Color 0,0,0 Text bx + bw/2,by + bh/2, "I'm a button !",True,True If MouseHit(1) Then ;pressed left mouse ? x = MouseX() ;get mouse coords y = MouseY() ;is the mouse was inside the button ? If RectsOverlap (bx,by,bw,bh,x,y,5,5) Then ;do something here End EndIf EndIf Flip Wend End Anyway, even if you are new to Blitz3D, I would really suggest you to take a look to a fantastic sprite library, which manages menu buttons very good, and not only. The library is called Sprite Candy, and you find here: www.x-pressive.com Regards, Sergio. |
| ||
or... Function Button(X,Y,T$,Width=200) If RectsOverlap (x,y,width,16,MouseX(),MouseY(),1,1) Then over = True Else over = False If over Then Color 0,100,255 Else Color 0,0,255 Rect x,y,width,16,True Color 255,255,255 Rect x,y,200,16,False Text x+(width/2),y+8,t$,True,True If MouseDown(1) And over Then Return True Else Return False End Function setbuffer backbuffer() repeat If Button(0,0,"Click Me!") = true then end flip until keyhit(1) |
| ||
Something I played around with a few years back. Simple, but it works.;Simple GUI Buttons Example ;Written in Blitz Basic 2D ;By Stephen Ancell ;February / 23 / 2003 AppTitle"GUI Buttons Example" Global gw=800,gh=600,gd=16,gm=2 Graphics gw,gh,gd,gm Type button Field x,y Field w,h Field func$ End Type create_button(0,100,50,25,"a") create_button(60,100,50,25,"b") create_button(120,100,50,25,"c") disp_button() Repeat detect_button() Until KeyHit(1) End Function create_button(x,y,w,h,func$) b.button=New button b\x=x b\y=y b\w=w b\h=h b\func$=func$ End Function Function disp_button() Cls Color 255,255,255 Text 0,0,"click on one of the boxes" Text 0,16,"press [Esc] to exit example" For b.button=Each button Color 0,100,150 Rect b\x,b\y,b\w,b\h,1 Next End Function Function detect_button() If Not MouseHit(1) Return EndIf disp_button() For b.button=Each button If MouseX()>b\x If MouseX()<(b\x+b\w) If MouseY()>b\y If MouseY()<(b\y+b\h) exec_func(b\func$) EndIf EndIf EndIf EndIf Next End Function Function exec_func(func$) Color 255,255,255 Select func$ Case "a" Text (gw/2),(gh/2),"button a" Case "b" Text (gw/2),(gh/2),"button b" Case "c" Text (gw/2),(gh/2),"button c" End Select End Function |
| ||
Thx guys |