selecting a portion of an image help

Started by
13 comments, last by namingway 16 years, 1 month ago
I have a map editor I'm writing and its coming along well, but I'm not sure how to select certain portions of the loaded tileset. The tile sizes are 32x32 and the tileset is loaded into a picture box, do I need a different component to be able to do this? Tilesets are a fix size of 8 across and 20 down or 256x640.
Advertisement
You could probably split the loaded image into multiple images, one for each tile, and display each with a picture box of it's own, or you could write some custom selection logic for the single picture box approach. What language and tools are you using to create this editor with?
Create-ivity - a game development blog Mouseover for more information.
This isn't something a API does for you, this is something you'll need to implement yourself in your map editor.

Here's how it might be done: First, store a bool somewhere, that's true when the user is selecting multiple tiles, and false when he's only grabbing one at a time. Next, have a button the user can turn on and off that controls that bool.
Then, when selecting tiles from the tileset, if the bool is false, get the tile as normal, but if the bool is true store wherever the player clicked, in terms of tiles. (Example: the player clicked on tilesheet X4 Y8) Don't select the tile yet, but wait for the player to click a second time, and store the second tile selected also. (Example: tilesheet X4 Y6)

If the second tile is less than the first tile, swap them, so the lowest numbered tile is first.(The tile higher up on the tilesheet)

Then, when the player clicks on the map to place a tile, check the bool. If the bool is false, place the single tile like normal, but if the bool is true, starting where the player clicked, cycle through the tiles from the tilesheet starting at the first place(in the tilesheet) the player clicked, and place them in the map until the second place(in the tilesheet) the player clicked is reached.
Cant beleive I forgot to say the language... It's C#, Splitting different tiles into separat pictureboxs would be a nice approach but I still get the probelm of cutting out the individual tiles to do so. My initial Idea was to check a click in the picture box and then detect the coordinates to see which tile was clicked but that will use alot of if elses...
Also an interesting idea Servant, I'll look into that as well.
It shouldn't use a lot of if-elses since your tiles are of a consistent size.

TileX = MouseX \ TileWidth
TileY = MouseY \ TileHeight

This will give you a 0-based X & Y indicator for which tile was clicked-on.

-Matt
www.mwgames.com - my game projects websiteNimble2D Blog - Simple 2D Game Dev with VB.Net
I'm trying your way but how do I get the mouse position from within the picturebox itself, my current attempt gets the global mouse position.
private void pictureBox1_Click(object sender, EventArgs e){            _tileX = MousePosition.X / 32;            _tileY = MousePosition.Y / 32;            if (_tileX == 1 && _tileY == 1)            {                _tileID = 1;                this.Text = "Mappy " + _tileID;            }}
I'm not familiar with how C# uses the normal Windows Form stuff, like cursor position per control, etc. Perhaps someone else can offer help in that area.

However, if MousePosition.X and .Y are the global positions ... can you subtract your Picturebox's top and left from those values to get the in-PictureBox value?

Once you have that, then instead of an If/Then or Switch to determine the Tile ID, you could do something like this ...

TileID = TileY * NumberOfTilesPerRow + TileX

I hope this is helpful.

-Matt
www.mwgames.com - my game projects websiteNimble2D Blog - Simple 2D Game Dev with VB.Net
Problem solved.
Quote:Original post by namingway
Problem solved.


How did you correct for the control-centric mouse position in C#?

-Matt
www.mwgames.com - my game projects websiteNimble2D Blog - Simple 2D Game Dev with VB.Net
I needed to use a mouse_click event then use the mouse args to track the position. Then by using code similiar to your suggestion it turned out perfectly. The problem I have now though is making something to put the tiles onto for a map. The code for its ready I just need to find out a way of making an array of picture boxs or something.

This topic is closed to new replies.

Advertisement