Reputation: 20127
So I'm creating something like a randomly generated maze in flash. For this, I fill the game space with blocks, and then have an algorithm that deletes certain connecting blocks to form a path.
When I generate the blocks, flash has no problem with putting them in offscreen points like (-1000, -1000), but then when I try to delete them afterwards to form a path, stage.getObjectsUnderPoint() returns nothing for these points.
How do I get objects under a point if they are not on the screen?
Upvotes: 0
Views: 228
Reputation: 461
Im assuming you have all these blocks stored in an array or vector. So you can check a point and loop through all your blocks an check what block is at that position. Example:
//return type could be whatever the blocks are
private function getObjectUnderPoint(p:Point):Object{
for(var i:int = 0; i < blocks.length; i++){
//checks for the block at position p
if(blocks[i].x == p.x && blocks[i].y == p.y){
return block[i];
}
}
//return null if there is no block under point p
return null;
}
In your case there would only be one block at a certain point so you dont have to return an array of blocks, but you could modify it to that if you wanted. Also, if you do not have your blocks stored in an array or vector, I would highly recomend that you do.
In order to make this more general you could pass the stage to the function. then check the children of that stage.
Example:
private function getObjectUnderPoint(s:Stage, p:Point):Array{
var objects:Array = new Array();
for(var i:int = 0; i < s.numChildren; i++){
var obj:Object = s.getChildAt(i);
//checks for the objects at position p
if(obj.x == p.x && obj.y == p.y){
objects.push(obj);
}
}
return objects;
}
the if statement inside the functions can also be changed to check whether the point p is located inside the objects, but this example only returns extact point locations.
Upvotes: 3