Hi,
I want to associate Bitmaps
with Points
, so I thought that using a Map<Point, Bitmap> would be a good idea, but how do I use it after setting it?
Here’s what I did:
myMap.set(new Point(bitmap.x, bitmap.y), bitmap);
After that, what am I suppose to write to get a bitmap from a specific coordinate?
myMap.get(PLEASE_HELP_ME);
Thank you!
You could use Map<Point, BitmapData>
, but then it will be based on the Point
instance, not the coordinates, so you would have to keep track of your Point
objects to look up your images.
The ideal might be if you can use Int
-based coordinates, perhaps you could use Map<Int, Map<Int, BitmapData>>
, like
var map = new Map<Int, Map<Int, BitmapData>> ();
if (!map.exists (x)) map.set (x, new Map ());
map[x][y] = bitmapData;
You can use Float
as a key value in a Dictionary
, but the risk with Float
values is that two numbers that are supposed to be the same could end up not matching due to minor little differences, or you could end up with so many key values that it bloats the data structure too much
Thank you for your reply!
I think it would work, but then, how can I access all bitmaps in a for loop? At first, I was doing this:
for (bitmap in myMap) {
...
}
But now, doing so will only get me to the Map
inside the Map
. How should I proceed?
Thank you for all the help!
You could do
var childMap;
for (x in map.keys ()) {
childMap = map[x];
for (y in childMap.keys ()) {
trace (x, y, childMap[y[);
}
}
…if you need the x
and y
values, in addition to the BitmapData
, otherwise, something like this would work if you only need the BitmapData
for (childMap in map) {
for (bitmapData in childMap) {
trace (bitmapData);
}
}
However, iterating over a Map
like this generates a new Array
every time you do it. As a result, I would strongly recommend that you use an Array
, List
or other data structure designed for iteration if you have to do it regularly. For example:
private var images:Array<BitmapData>;
private var imageByPosition:Map<Int, Map<Int, BitmapData>>;
On that note, you could use an Array
for this instead of Map
if you want
var images = new Array<Array<BitmapData>> ();
if (images[x] == null) images[x] = new Array ();
images[x][y] = bitmapData;
I think this would work, the question would be what value you get if you reference an index that has not been used yet