Actionscript 3: get display object at pixel
Asked Answered
C

2

7

How can I enumerate display objects under a pixel relative to the stage?

Clarification: I want to write a function which get (x,y) as input and returns an array of objects as output.

update: I want to avoid looping over all the display objects, to tell which one is under the specified pixel.

Chloramphenicol answered 14/4, 2009 at 14:4 Comment(2)
Can you add some more detail please?Impound
Input: x,y. Output: array of objectsChloramphenicol
V
16

Any DisplayObjectContainer (such as a MovieClip or the stage) has a method called getObjectsUnderPoint that returns an array of display objects under that point. It takes a Point object as an argument.

var myObjects: Array = stage.getObjectsUnderPoint(new Point(5, 5));

If you are using it in a class don't forget to import flash.geom.Point;

Venesection answered 14/4, 2009 at 14:42 Comment(1)
Nice, I didn't know that one.Amalle
A
0

The hitTestPoint() method will get you information if a DisplayObject is at the specified point (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#hitTestPoint())

To get a list of objects you would have to recursively investigate all children of the stage, I don't think there is a predefined function for that.

Alternatively: What are you trying to achieve? Maybe you can just use events and make them bubble?

Amalle answered 14/4, 2009 at 14:11 Comment(2)
This is problematic, because this way I would have to call hitTextPoint to all display object added to the display list, and it would slow things down. I just want objects to react to nearby objects.Chloramphenicol
Ah, as Bryan points out, there actually is a method to get a list!Amalle

© 2022 - 2024 — McMap. All rights reserved.