Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Mouse position for selecting tiles to paste in editor

Author
Message
Venatusio
12
Years of Service
User Offline
Joined: 6th Mar 2012
Location:
Posted: 28th Mar 2012 15:41
Hi all,

I am writing a tile editor, more than anything it something I can get stuck into to learn the code better. anyway...

I open bitmap A which has a nice grid drawn on it 25 boxes across and 4 down I then open the sprite sheet and then grab the tile images out of it and paste them into the boxes and finally paste that to the bottom of the screen. The idea being I can simply click the tile and paint away with it.



They are all spaced out equally and I am using mousex() mousey() to work out what the mouse is over when clicked. Problem is I have made it for a sprite sheet 25 across and 4 down (100 tiles in all) (I am going to tweak it later so you can load in a different tile sheet if needed - anyway...)

100 tiles is 100 mouse detect commands e.g.



(the adj is the 38 pixel adjustment between each tile)

I get the feeling I am missing something and maybe there is a much smarter way to do this.

Just thought I would throw it out to the pros here to see if they can point me in the right direction.

Thanks in advance for any help/tips
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 28th Mar 2012 18:41
Oh lol, imagine doing it that way for my level editor! It's 256x256 tiles (65536 in total).

I'm assuming you have the tiles saved in an array, something like this:



If not, then the hairs on my back only stand up in thought of what you could have done instead. You should probably adopt the system above.

Next step is very simple. You just have to translate the mouse coordinates to tile coordinates. Here's the code:



Aaand there you go

TheComet

Venatusio
12
Years of Service
User Offline
Joined: 6th Mar 2012
Location:
Posted: 29th Mar 2012 00:45
Hi Comet,

I am using an array to track what tiles are on the map.

But your code seems to be to work out where you are over the map and placing a tile. (it looks similar to mine)

I was after a way to select the tile I want to use from a selection at the bottom of the editor and they are not up against each other (look at image) they are spaced out, so dividing by the tile size does not quite work.

That said I think you have pointed me in the right direction, if I write a formula to divide by tile size and take into account the 7 pixel gap between each tile I should be able to work out what number tile across the mouse is over.

Ahhhh should have paid more attention at maths in school

Thanks for your help.

Cheers,
Venatusio
Venatusio
12
Years of Service
User Offline
Joined: 6th Mar 2012
Location:
Posted: 30th Mar 2012 05:20
I think I may have found an even better way of doing this using sprite collision.

- Paste the tiles onto the tile selection grid as sprites.
- Put a small hidden sprite under the mouse pointer and have it follow the mouse.
- Wait for mouse click then check for a sprite collision
- If you use sprite collision and only pass the number of the hidden sprite (under the mouse pointer) the command returns the number of the sprite it collided with.
- Then you can set the selected tile to that number. (So when you click in the map to place a tile it uses then one you last clicked on)

I haven't coded it yet but cannot see why it wont work.
Venatusio
12
Years of Service
User Offline
Joined: 6th Mar 2012
Location:
Posted: 4th Apr 2012 03:43
I think I am stuck in a Noob Loop

Every couple of days I discover a better way to do the code I have already done lol
Ramon156
13
Years of Service
User Offline
Joined: 13th Jul 2011
Location: Netherlands
Posted: 4th Apr 2012 17:27
Quote: "I think I am stuck in a Noob Loop "


Thought I was the only one
Mage
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 5th Apr 2012 08:46 Edited at: 5th Apr 2012 09:00
Here's a tip that might make this easier if you don't wanna make things complicated but you still want some speed.

Copy the mouse input into a variable and test the variable instead.

mX = MouseX()
mY = MouseY()
mClick = MouseClick()


This will be faster, but not that complicated. I think you can do this with sprite collision but that's not necessary.
Since you have a grid, it's better to use my method above, and merely calculate based on mouse coords what button was pressed if any.
This can be done with just 2-3 lines of code. Is simpler and easier to read.


Edit: Solution would look something like this...

mX = MouseX()
mY = MouseY()
mClick = MouseClick()

if mClick = 1
If mX > GridLeftX and mX < GridRightX and mY > GridTopY and mY < GridBottomY

ButtonWidth = 38
GridSelectionX = (mX - GridLeftX) MOD ButtonWidth
GridSelectionY = (mY - GridTopY) MOD ButtonWidth
ButtonNumber = GridSelectionY * 25 + GridSelectionX


Venatusio
12
Years of Service
User Offline
Joined: 6th Mar 2012
Location:
Posted: 7th Apr 2012 12:26
That approach works really well, as you said it simple and easy to read.

Thanks for your help Mage

Login to post a reply

Server time is: 2024-11-22 04:25:02
Your offset time is: 2024-11-22 04:25:02