How to identify a selected sprite?

Previous topic - Next topic

laddsa

Hi, if I have many sprites on-screen, is it possible to return the sprite id of the sprite that i click on using the mouse?

Thanks,

Laddsa

Ozden79

There is no direct way as far as I know and you can check whether mouse x and y coordinates are within the bounds of any of your sprites. To do this, you'll need to note down the last bounds of your sprites or maybe should check when you calculate where to draw your sprite.

Kitty Hello

Again - there is no "sprite". It's a bad naming from day 1 and if should read "blit". You can just draw images at a position. To test for collicion you either use BOXCOLL or SPRCOLL with the coordinates of the sprite's position.

Schranz0r

Kitty Hello say all you need...
And its in the wrong forum ;)
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

laddsa

Many thanks guys, I shall try your suggestions.

Amon

Quote from: laddsa on 2010-Feb-10
Hi, if I have many sprites on-screen, is it possible to return the sprite id of the sprite that i click on using the mouse?

Thanks,

Laddsa

What I do is the following. If you have say a type with sprite id's you can use mouse math to find which one is clicked. e.g:

Code (glbasic) Select
// --------------------------------- //
// Project: SpriteSelect
// Start: Thursday, February 11, 2010
// IDE Version: 7.250


SETCURRENTDIR("Media") // seperate media and binaries?

SETSCREEN 800,600,0

GLOBAL image = 0
GLOBAL MouseX, MouseY, MB1, MB2
SETTRANSPARENCY RGB(255,0,255)
LOADSPRITE "sprite.png",0
LOADSPRITE "arrow.png",1

GLOBAL tempid = 0
GLOBAL ActualID = -1

TYPE Sprite
x
y
id
ENDTYPE

GLOBAL SpriteArray[] AS Sprite

FOR x = 0 TO 5
GLOBAL s AS Sprite
s.x = RND(800)
s.y = RND(600)
s.id = tempid
DIMPUSH SpriteArray[],s
tempid = tempid + 1
NEXT


WHILE KEY(01) = FALSE

MOUSESTATE MouseX, MouseY, MB1, MB2

DrawSprites()
SelectSpriteID()

DRAWSPRITE 1, MouseX, MouseY

SHOWSCREEN
WEND

FUNCTION DrawSprites:
FOREACH s IN SpriteArray[]
DRAWSPRITE image,s.x,s.y
NEXT
ENDFUNCTION

FUNCTION SelectSpriteID:
FOREACH s IN SpriteArray[]
IF MouseX - s.x >0 AND MouseX - s.x < 40
IF MouseY - s.y >0 AND MouseY - s.y < 40
ActualID = s.id
ENDIF
ENDIF
NEXT

PRINT ActualID,20,20
ENDFUNCTION


[attachment deleted by admin]
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Schranz0r

If you have overlapt sprites, then that give some problems :)


@ Laddsa:

You write a Mapeditor right?
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

laddsa

@ Amon:
That worked great! Many thanks for the code! :good:

@Schranz0r:
Not quite sure what a mapeditor is!  :(