[4.5.6] Finding the x & y coordinates of a tile [solved]

JFranco

New member
Question: is there a way to determine a tile's x & y position using .asm? For example, I have a hurt tile, and I'd like to know it's x and y position via .asm, and not looking at it's location on-screen.

Answer:

In a tile collision asm script, tileX and tileY variables are the coords (in pixels) of that collision (player and that tile)
And if you want to know the tile X,Y number (like the « X: » and « Y: » from the tool)

You can use:
Code:
LDA tileX
AND #%111100
STA temp1

now you have a X: of the tile in temp1

Same thing can be done with tileY

----

Thank you, DALE COOP, for the answer!
 

JFranco

New member
A few corrections: (from Joseph Rossi)

The number ANDed is missing a couple of zeroes. It should be #%11110000. Or you could use hexidecimal #$F0.
Dale's code would be the first step in getting the proper index to be put into either the y or x register to then get the tile collision value in one of the collision tables (e.g. collisionTable,y), but it's missing some steps to get you the x tile position. Let's say that the tile your are looking at is (5, 9), Dale's code will give you in hexidecimal #$50. To get it to become a 5, you need to use LSR 4 times before storing it into temp1 or whatever variable you're using. So the code should look like this:
Code:
LDA tileX
AND #%11110000
LSR
LSR
LSR
LSR
STA temp1

If you are trying to get the index for the for the collision table, the code should look like this:
Code:
LDA tileX
AND #%11110000
LSR
LSR
LSR
LSR
STA temp1
LDA tileY
AND #%11110000
ORA temp1
TAY
LDA collisionTable,y
Additionally you would have to do some kind of a check to make sure you are looking at the correct collision table.

Lastly, and I'm away from my computer right now, so I might not get this part 100% right, but tileX and tileY don't always contain the values of the pixel position of the tile collision. This might not be true for all modules but was definitely true in the platformer module. The issue is either just with tile type 01 or all tile types after 01. I think it's the former, but I forget off the top of my head.

The problem is that the way the tile collision code works is something like this: it checks all four of the object collision points, storing the results into tempA to tempD as it goes. It then checks if any of those tiles were tile type 01 and if so, it goes ahead and does the solid collision stuff. If it wasn't, it checks the collision points again, this time breaking to handle the collision code if the collision type is not zero.

Again, I'm writing this from memory, so I might have some details backwards, but the point is that there is an issue with tileX and tileY not having the correct values for a certain tile type or certain tile types. I think it's just tile type 01 that will always give you the coordinate of the lower right collision point because that is the last point that was checked even though the tile collision in question may have been one of the other 3 points.
 

dale_coop

Moderator
Staff member
Joseph is right, I missed some steps while writing the answer on FB.... (it's the reason we should never write code on FB, it's not easy to focus, test the script and write it down correctly)

Thank you jorotroid for correcting me ! <3
 
Top Bottom