2

I'm a bit new with AS3 (but not really with coding) so please forgive my ignorance. I'm creating a small function that will be called by a Main Function to update the position of 52 Pointers that have the x and y position of multiple point objects (empty movie clips). It will also then update two global arrays with those values (one array for the x and one for the y).

The problem is, as there are 52 of them, and they will probably grow in quantity, I'd like to be able to use a FOR function to do it, but I can't seem to be able to figure it out.

I get this error: Access of undefined property _point.

Here is a piece of the code that dream about:

function happyFunc():void
{
    var avpointers:int = 52;
    var vpointx:Array = new Array();
    var vpointy:Array = new Array();        
    for (aa=0; aa<vpointers; aa++)
    {
        vpointx[aa] = _point[aa].x;
        vpointy[aa] = _point[aa].y;
    }
}

And this is the code that I'm stuck with...

function reallySadFunc():void
{
_point1 = localToGlobal(new Point(point1.x,point1.y));
//...
_point52 = localToGlobal(new Point(point52.x,point1.y));
vpointx[0] = _point1.x;
vpointx[1] = _point2.x;
//...
//oh god there are 104 lines of this why do I have to suffer
}

Thank you!

  • What is `point1`, `...` and `point52`? Are those `MovieClip` objects on the stage? – xxbbcc Nov 14 '14 at 01:45
  • They are Point objects that represent a location. In my code, they are the global coordinates of point1 to point 52, which are MovieClips in a MovieClipt on the Stage. For more on Points: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Point.html – John Schwimmer Nov 14 '14 at 01:59
  • How do you get these points? They cannot be on the stage so you have to create them somehow. – xxbbcc Nov 14 '14 at 02:00
  • I add them by placing an empty MovieClip on the Stage. (e.g. press Ctrl+F8 and don't do anything), then name then "point1" (instance name) – John Schwimmer Nov 14 '14 at 02:03
  • Ah ok, so they aren't `Point`-s, they're `MovieClip`-s. – xxbbcc Nov 14 '14 at 02:03
  • There are the point MovieClip-s and there are the _point Point-s. The point MovieClip-s are used to feed the _point Point-s with their x and y information. Which is what I need to update in the sub function whenever I want. – John Schwimmer Nov 14 '14 at 02:07

1 Answers1

0

If I read your question correctly, I think this is what you need:

public static const CLIP_COUNT:int = 52;

// ...

private function happyFunc(parentClip:DisplayObjectContainer):void
{
    var vpointx:Vector.<Number> = new Vector.<Number>(CLIP_COUNT, true);
    var vpointy:Vector.<Number> = new Vector.<Number>(CLIP_COUNT, true);

    var clipPoint:Point = new Point ();
    var globalPoint:Point;

    for (var i:int = 0; i < CLIP_COUNT; i++)
    {
        var childClip:DisplayObject = parentClip.getChildByName("point" + 
            (i + 1).toString());

        clipPoint.x = childClip.x;
        clipPoint.y = childClip.y;

        globalPoint = parentClip.localToGlobal(clipPoint);

        vpointx[i] = globalPoint.x;
        vpointy[i] = globalPoint.y;
    }

    // do something with vpointx and vpointy - they're local variables
    // and will go out of scope unless you declare them as class members
    // or somehow return them from this function.
}

This function works by taking the parent display object (the one that contains the 52 movie clips - this could be the Stage) and iterates through them by getting each movie clip by name. The code assumes that your movie clips are called point1, point2, ..., point52.

To speed up the local-to-global coordinate conversion, two Point objects are created and then reused during the for loop to avoid creating many temporary Point instances.

Instead of using Array, use Vector.<Number> - this class has better performance than Array does.

xxbbcc
  • 16,930
  • 5
  • 50
  • 83