Imaginary Area

4 posts

Flag Post

Hey, I was wondering if it would be possible to create something similar to a Textfield variable but is as if you are selecting a certain area. Say SelectionArea is a type of variable so it would go something like this:

var blah:SelectionArea = new SelectionArea();


Something like that. If there is anything similar to this in anyway, please tell me, it would help me a lot.

Flag Post

I would use a Rectangle for that. Let’s assume you are making an RTS game and want to make it possible to select multiple units by drawing a rectangle around them, that’s a fairly classic use of selection areas. I’d recommend doing:

selectionArea:Rectangle, a permanent variable that you initialize when you initialize the game.

in the function that handles MouseEvent.MOUSE_DOWN events add:

selectionArea.x = screenToMapX(mouseX);
selectionArea.y = screenToMapY(mouseY);

where screenToMapZ is a function that converts a coordinate Z = X or Y on the screen to the corresponding position on the game map, based on where the view currently is.

In the function that handles MOUSE_UP events add:

temp = screenToMap(mouseX) - selectionArea.x;
if (temp > 0) selectionArea.width = temp;
    selectionArea.x += temp;
    selectionArea.width = -temp;
//same with height.
if (selectionArea.width >= minimalSelectionWidth && selectionArea.height >= minimalSelectionHeight)
// Reset the selection area by making its x, y, width and height variables 0.

where selectUnitsIn is a function that loops over all units on the map and for each unit:
Deselects it.
Checks if it is controlled by the player and located inside the selection rectangle and if so, selects it.

Hope this helps.

Flag Post

To get into more detail with this.

The game I am attempting to make has counties which are inside of territories, I need to have it so for each county, there is a box around it that is 1 pixel bigger in length and width. This would allow me to find adjacent territories for sending “troops.”

So I need it so:

countyAdj.width = county.width + 2;
countyAdj.height = county.height +2;
countyAdj.x = county.x + 1;
countyAdj.y = county.y + 1;

So it would create a box with a pixel border around the county.

Flag Post

I’m not exactly sure what you are trying to achieve, but countryAdj.x should get country.x – 1 and CountryAdj.y should get country.y – 1 in that example. If you are trying to find adjacent territories, perhaps you could store the territories in a grid. Or have each territory have references to the territories that it is adjacent to. Again, I’m not sure what the task is: mind posting a picture/diagram of some sort?