Author Topic: How to identify a selected sprite?  (Read 3044 times)

Offline laddsa

  • Mc. Print
  • *
  • Posts: 3
    • View Profile
How to identify a selected sprite?
« 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

Offline Ozden79

  • Mr. Polyvector
  • ***
  • Posts: 121
    • View Profile
Re: How to identify a selected sprite?
« Reply #1 on: 2010-Feb-10 »
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.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10753
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: How to identify a selected sprite?
« Reply #2 on: 2010-Feb-10 »
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.

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5069
  • O Rly?
    • View Profile
Re: How to identify a selected sprite?
« Reply #3 on: 2010-Feb-10 »
Kitty Hello say all you need...
And its in the wrong forum ;)
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline laddsa

  • Mc. Print
  • *
  • Posts: 3
    • View Profile
Re: How to identify a selected sprite?
« Reply #4 on: 2010-Feb-11 »
Many thanks guys, I shall try your suggestions.

Offline Amon

  • Mr. Drawsprite
  • **
  • Posts: 55
  • I code, therefore I am!
    • View Profile
    • Amon.co
Re: How to identify a selected sprite?
« Reply #5 on: 2010-Feb-12 »
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!

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5069
  • O Rly?
    • View Profile
Re: How to identify a selected sprite?
« Reply #6 on: 2010-Feb-12 »
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 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline laddsa

  • Mc. Print
  • *
  • Posts: 3
    • View Profile
Re: How to identify a selected sprite?
« Reply #7 on: 2010-Feb-13 »
@ Amon:
That worked great! Many thanks for the code! :good:

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