getting mouse coordinates

enonod

Well-Known Member
Licensed User
Longtime User
I seem to be having a lot of trouble finding how to get mouse coordinates when clicking on, for example, an image or a gamewindow. It feels as if my brain has turned off.
Any pointers please?
 

enonod

Well-Known Member
Licensed User
Longtime User
I am grateful agraham, I don't know what I would do without you.
I am not familiar with using the libraries but I am sure it will go smoothly.
It did not occur to me that the mouse might be 'colliding' with the window.

[Edit]Ammend that, external libraries. I see that Door is an internal, what a peculiar name? Must be access to something, which I am sure will become clear.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The Door library allows you to access .Net Framework features without creating a custom library. The downside of this library is that it requires some knowledge of the .Net Framework.
Here is the code that catches the MouseDown event of an Image control:
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    obj.New1(False)
    obj.FromControl("image1")
    MouseDownEvent.New1(obj.Value, "MouseDown")
End Sub

Sub MouseDownEvent_NewEvent
    obj.Value = MouseDownEvent.Data
    x = obj.GetProperty("X")
    y = obj.GetProperty("Y")
    form1.Text = x & " " & y
End Sub
The source code is also attached as a file.
 

Attachments

  • 1.sbp
    788 bytes · Views: 236

enonod

Well-Known Member
Licensed User
Longtime User
Thank you for that Erel. In fact I made a mistake, the background image I am using in the game window is actually a sprite. When I click on the image (sprite) I do not trigger the event (collisionmouse), however if I click on a sprite passing over the background sprite I do trigger it.
I would appreciate knowing if there is something different if a sprite is used for a background.
Thank you.
[Edit] I don't think I would have got there without your code, I simply started using gw.X etc.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Don't confuse an Image (Bitmap) with an Image control. In this case I don't think you have an Image control so the Door library won't work.

If the image is actually a Sprite then clicking on it should raise a CollisionMouseEvent and you can get the coordinates from the X and Y GameWindow properties. You can use the new ID property in the 1.21 version of the Sprite library I just posted. to identify the individual Sprite returned by the Sprite1 property.

EDIT:- How are you using a Sprite as a background?
 

enonod

Well-Known Member
Licensed User
Longtime User
I think I have been at it too long today, I have had many experimental versions while experimenting and got out of sync.
In fact I used an Image on a GameWindow...
B4X:
gw.New1("Form1",0,0,321,321)
   gw.DrawBackgroundImage(AppPath & "\grid 20x20.png")

(Almost a sprite?)

I have sprites on the image and the CollisionMouse suggested by you works with them. It does not work with the BackGround Image and doesn't work with Door.
I guess I have dropped between the two, or... am I clicking on the window.
 

agraham

Expert
Licensed User
Longtime User
That's not an Image control nor a Sprite, you are merely using an image file as a background. As it looks like a small image I would imagine it is being stretched to fit the GameWindow. Clicking on the GameWindow background should raise the unfortunately named NoCollisionMouse event.
 

enonod

Well-Known Member
Licensed User
Longtime User
The gamewindow is the same size as the image, it is a windowed screen. [Edit] Now I see why you thought it was small it is 20x20 but each is 15 pixels.
Thank you, you have guided me to the oddly named event which does work.

B4X:
Sub gw_NoCollisionMouse
Msgbox("yes")
   label3.Text=gw.x
   label4.Text=gw.y
End Sub

Thanks, now to look at your new Sprite library which I presume I just drop into Libraries folder.
 
Last edited:
Top