|
Post by OddChild on Oct 21, 2006 12:05:38 GMT -5
that would be neat if images had, an onclick event... so that way a user can click on items in a room, like the old siera games etc... or just having the ability to click on images, as buttons... then have it go to a sub etc..
|
|
|
Post by Guilect on Oct 22, 2006 11:00:59 GMT -5
It would be nice but images do not have any windows events that can be monitored. The best you can do is to check for an intersection (collision) between two items.
While I can think of several ways to accomplish this, using the built-in collision detection between two images might be the easiest.
For example you can load a custom mouse cursor as one image and then check to see if this collides with another image whenever the mouse button is clicked. (this is much like the example 'menu' in the examples folder). (also look at u9's racer menu example)
If you don't want to show a custom mouse cursor you can load a single pixel image for the mouse position and choose not to make it visible.
Other methods include simply doing the math to see if a point is in a rectangle or not; or using get pixel to check to see if the mouse position is over a particular color (item) or not (background).
|
|
|
Post by u9 on Oct 22, 2006 13:09:49 GMT -5
Here's a demo on how you can read a mouse click. This one consideres the a box clicked when the mouse key is pressed down. Real buttons often consider it a click when the mouse button is released again.
Option explicit randomize
' On-Click Demo
' Some boxes Class Box Public x Public y Public width Public height Public name Public alpha End Class
graphics.initialize mouse.initialize key.initialize
Dim font font = graphics.createFont( "System", 12, True )
' Create some boxes Dim boxes(5) Dim i For i = 0 To 5 Set boxes(i) = New Box boxes(i).x = cint(rnd * 620) boxes(i).y = cint(rnd * 460) boxes(i).width = cint(rnd * 50+50) boxes(i).height = cint(rnd * 15+15) boxes(i).name = "Box " & i boxes(i).alpha = cint(rnd * 50+100) ' How transparent is the box Next
Dim isRunning, isMouseDown isMouseDown = mouse.leftButton ' Current mouse button state isRunning = True
Do While isRunning = True ' Keyboard input If key.Pressed(0) Or key.pressed(1) Then isRunning = False ' Draw some boxes graphics.clear For i = 0 To 5 Call graphics.setrect( boxes(i).x, boxes(i).y, boxes(i).width, boxes(i).height, argb(boxes(i).alpha, 100, 200, 255) ) Call graphics.settext( boxes(i).name, boxes(i).x, boxes(i).y, font, argb(255, 200, 0, 0) ) Next graphics.display ' Mouse button released? If isMouseDown And mouse.leftButton = False Then isMouseDown = False Call graphics.settitle( "No Boxed Clicked" ) End If ' Mouse button clicked If isMouseDown = False And mouse.leftButton Then For i = 0 To 5 If mouseOnBox( boxes(i) ) Then Call graphics.settitle( "Box " & i & " Clicked" ) End If Next isMouseDown = True End If Loop
key.terminate mouse.terminate graphics.terminate
' Return true if mouse is within the box b Function mouseOnBox( b ) Dim x, y x = mouse.x y = mouse.y ' Check if mouse is within the limits of the box (both horizonally and vertically) If b.x < x And b.x+b.width > x And b.y < y And b.y+b.height > y Then mouseOnBox = True Else mouseOnBox = False End If End Function
|
|