Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Click on sprite

Author
Message
Qoheleth
13
Years of Service
User Offline
Joined: 11th Jun 2011
Location:
Posted: 29th Sep 2011 19:12
I've searched, but could not find an answer to what I thought might be a very common question. How do I tell DBP to determine if I clicked on a specific sprite? Using MouseX, MouseY and MouseClick are easy *if* your sprites are rectangles, but mine are irregular shaped (OK, not so irregular - they are triangles). Help will be appreciated as I am now stuck in my program, and I hope I don't have to go into intricate pixel math just to determine if the click happened somewhere inside the confines of the sprite.
SimpleProgram
13
Years of Service
User Offline
Joined: 2nd Oct 2011
Location: Cali
Posted: 3rd Oct 2011 03:15 Edited at: 3rd Oct 2011 21:32
Simple, all you must do is hide the regular mouse, replace it with a sprite located at mouseX() and mouseY() to make the sprite act as a new mouse, if the mouse sprite and the target sprite hit and if you then click you'll get click able sprite by imbedding two IF statements, here is what the whole code snippet should look like in your loop/program:

function click_on_sprites()

hide mouse `hide the mouse

mouseX() = Mx : mouseY() = My : mouseclick() = Mc
`(give these variables for easy local access , not global)

global mouseimage as integer = 1 `only use global if youre going to use this fucntion throughout the program
global mousesprite as integer = 1

load image "media\mousesprite.bmp", mouseimage
`(edit the file location to appropriate location)

sprite mousesprite, Mx, My, mouseimage
`paste the sprite on the screen at the current location of the mouse

If sprite hit(mousesprite, targetsprite) = 1 `if mouse sprite hits then target sprite
If Mc = 1 `if you then clicked the left mouse button
(put your desired code here) `execute desired code
Endif
endif

endfunction

Sprite hit() not sprite collision() will make it a more accurate result of odd shaped sprites.

do not use "change mouse" as it will not make the mouse a sprite but only change the image of the mouse itself

GOOD LUCK!

Simple knows best.
Qoheleth
13
Years of Service
User Offline
Joined: 11th Jun 2011
Location:
Posted: 5th Oct 2011 10:46
Thanks, Simple, I'm going to try it.
SimpleProgram
13
Years of Service
User Offline
Joined: 2nd Oct 2011
Location: Cali
Posted: 5th Oct 2011 20:02
Your welcome, tell me how it works out.

Simple knows best.

Login to post a reply

Server time is: 2024-11-21 15:53:10
Your offset time is: 2024-11-21 15:53:10