[SOLVED] Make a random red point appear on a map


#1

Hey everyone !

So, I have a question : If I take the world map and i want to create a red point that can appear only on land and not on sea, how do I do ?

It should look like this

And if there’s lake and other stuff on the map where the point can’t appear on, how do I make that happen too ?


(Martí Angelats i Ribera) #2

How about make random points, check if it is on land. If it not try again.

Becouse the random function is actually pseudo-random, it won’t take long to find a point wich is in the land.


#3

Ok, then how do I check if it’s on land or on water ?


(JP Mortiboys) #4

Given the picture you posted, how can you tell if the point is on land or water?


(Martí Angelats i Ribera) #5

If i were you i would do this:

Create an image with the land parts in white and the other parts in blue. Then use the bitmap to test if the point is in land or whater (using integers so maybe you’ll have to round).

private var _map:BitmapData;
private function isLand(x:int = 0, y:int = 0):Boolean
{
	return _map.getPixel(x, y) == 0xFFFFFF;
}

private function getLandPoint():Point
{
	var point:Point = new Point;
	do
	{
		point.x = Math.random * _map.width;
		point.y = Math.random * _map.height;
	} while (!isLand(Math.floor(point.x), Math.floor(point.y)));

	return point;
}

Obviously you have to load the image to that bitmap once.

I hope it helps


(Zachary Lewis) #6

With that code, the point would never appear on a border.

It would probably be safer to check if the point isn’t on water.


(Martí Angelats i Ribera) #7

The key here is to create a black-white map. There are no borders (it’s not the same image).


#8

Thanks for the replies, i get the idea on how to make it work. But what if my map has more than one color for the water/land ?

Here is the map i’ll use in my game. Is there a way to make it work for this kind of map ? If i do an another version of the map with only 1 color for the water and another for the land and put it in a lower layer than the original map, could getPixel() still work ?


(Martí Angelats i Ribera) #9

I think you have two options:

  1. Use intervlas in the 3 color chanels
  2. Make a second map that never is rendered but it is used to test

(Zachary Lewis) #10

With a bit of Photoshop magic, you’d get something like this:

If you want to be able to determine what country you’ve hit, you can color-code them (like @Copying suggested), and you’d get something like this:

With that one, if the value of the blue channel is anything other than 0, you’ve hit water. If you’ve hit land, you can check the value of the green channel. I’ve set them to 0xa0, 0xb0, 0xc0, et cetera.


#11

It works like a charm now. Huge thanks for the help !