Currently Selected Tile (Types)

Blitz3D Forums/Blitz3D Beginners Area/Currently Selected Tile (Types)

Matt Vinyl(Posted 2007) [#1]
Hi all,

I've a 5x5 'grid' of tiles, each of which is a type element (Fields: X, Y, Letter). Each tile is a randomly selected letter of the alphabet.

When the player moves the cursor over a tile, how can I 'read' what the tile they are over is?

I hope this makes sense, but if any further info. would be useful, please let me know.

Any help is much appreciated! ;)

Kindest regards,
M


Curtastic(Posted 2007) [#2]
Well you have Dim grid.tile(5,5) I hope.

Then you convert the mouse position to a tile index.
Local mousetile.tile
mousetile=grid(mousex()/tilewidth,mousey()/tileheight)

Now mousetile points to the tile the mouse is over.
Print mousetile\letter


Matt Vinyl(Posted 2007) [#3]
Hi,

Hmm, I didn't have the array set up, so I'll introduce that and see if I can get it to work. In the mean time, here's my code (relevant parts)

; Variable & Type Dimensions-----------------------------------------------------------------------------------------------------

Global state=0 ; Game state variable.
Global mx,my ; Mouse X and Y co-ordinates.
Type tile
Field x,y,l
End Type

; Media--------------------------------------------------------------------------------------------------------------------------

Global gfxpoint=LoadImage("gfx\hand.png") : MaskImage gfxpoint,255,0,255
Global gfxboard=LoadImage("gfx\board.png")
Global gfxletters=LoadAnimImage("gfx\letters.png",129,128,0,78) : MaskImage gfxletters,255,0,255

; Main Game Loop-----------------------------------------------------------------------------------------------------------------

Repeat

	Cls
	If state=0 Then
		Generate()
	End If
	Display()
	Flip

Until KeyHit(1)

; Functions----------------------------------------------------------------------------------------------------------------------

Function Display()
	DrawImage gfxboard,0,0
	mx=MouseX()
	my=MouseY()
	mxa=Floor((mx-53)/129)
	mya=Floor((my-62)/128)
	For letter.tile=Each tile
		DrawImage gfxletters,letter\x,letter\y,letter\l
		If mx>=53 And mx<698 And my>=62 And my<702
			DrawImage gfxletters,(mxa*129)+53,(mya*128)+62,letter\l+26
		End If
	Next 
	DrawImage gfxpoint,mx,my
End Function


Thanks!


Curtastic(Posted 2007) [#4]
The best way to do it is, when you create the tiles you do something like:
letter.tile=new tile
grid(x,y)=letter
letter\x=x
letter\y=y


Then to tell which tile the mouse is over you use grid(mxa,mya)


Curtastic(Posted 2007) [#5]
Or just change the If statement you have to:

If mx>=letter\x And mx<letter\x+129 And my>=letter\y And my<letter\y+128
	DrawImage gfxletters,(mxa*129)+53,(mya*128)+62,letter\l+26
End If