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
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.
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.
Kitty Hello say all you need...
And its in the wrong forum ;)
Many thanks guys, I shall try your suggestions.
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:
// --------------------------------- //
// 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]
If you have overlapt sprites, then that give some problems :)
@ Laddsa:
You write a Mapeditor right?
@ Amon:
That worked great! Many thanks for the code! :good:
@Schranz0r:
Not quite sure what a mapeditor is! :(