0

In the code below, I have created 4 sprites (picn) inside a sprite (noteholder). How can I get the absolute values of the picn instances that I create? I know about the localToGlobal function, but I can't figure out how to use that in this case. At this point, I think I need the container because I need to be able to move the sprites after creation. Any help is greatly appreciated. Thanks!

package 
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import Main;


public class Notes extends Sprite
{
    private var speed:int = 14;
    [Embed(source="../lib/Dodgethis.jpg")]
    private var picn:Class;
    private var noteholder:Sprite = new Sprite();


    public function appear() {
        trace ("appear ran")
        var arr1:Array = new Array;
        var numnotes:Number = 4;
        Main.StageRef.addChild(noteholder);
        trace (noteholder.x, noteholder.y);


        for (var i = 0; i < numnotes; i++)
            {
            //trace (i);
                var nbm:Bitmap = new picn;
                noteholder.addChild(nbm);
                nbm.y = i * 50;
                arr1.push(nbm);
Andy G
  • 19,232
  • 5
  • 47
  • 69
  • Consider this http://stackoverflow.com/questions/6062209/flash-as3-understanding-localtoglobal – Mark Fox Sep 09 '13 at 05:17
  • Thanks, but I can't reference the point I want to use the localToGlobal method on because it isn't created at runtime and I have no way to reference it by name (at least I can't figure out how to reference them by name due to variable scope) – something13 Sep 09 '13 at 19:07
  • Given your code above you can reference these bitmaps two ways — either via the array like `arr1[0]` or from the sprite `noteholder.getChildAt(0)` — you should probably prefer `arr1` and you should probably scope it to the object and rename it `noteSprites` or something clearer. You don't need really need to mess around with naming dynamically created displayobjects. – Mark Fox Sep 09 '13 at 23:58

2 Answers2

0

You can use getBounds about getBounds

// call this after added to stage
if (stage) {
     var r:Rectangle = nbm.getBounds(stage);// r's x and y is the global pos
}

And I think localToGlobal should work in your case. Move the sprites don't have effect on localToGlobal

Pan
  • 2,101
  • 3
  • 13
  • 17
  • Sorry, but this didn't work for me. If I typed it in exactly as you have it, nothing happened. The script never ran. When I took out the if (stage) it ran, but the y values were all 0, which doesn't help me at all. Have any idea what else to try? – something13 Sep 09 '13 at 19:05
  • See my comment above and try `noteSprites[0].getBounds(Main.StageRef)` – Mark Fox Sep 10 '13 at 00:03
0

You need to hold a reference to the display objects you dynamically create — you don't need a "name" which is just an abstraction of instance variables when creating visual assets in the Flash IDE.

I've updated your code to demonstrate the concept:

package 
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Stage; // added this
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle; // added this (see below)
import Main;

public class Notes extends Sprite
{
    private var speed:int = 14;
    [Embed(source="../lib/Dodgethis.jpg")]
    private var NoteBMP:Class;
    private var display:Sprite = new Sprite();
    private var notes:Array = []; // make notes an instance variable, so it's accessible throughout the class

    // I renamed appear to 'build' because I'm a pedantic moron
    public function build():void
    {
        trace ("build ran");
        var noteCount:int = 4;
        Main.StageRef.addChild(display);

        for (var i:int = 0; i < noteCount; i++)
        {
            var nbm:Bitmap = new NoteBMP;
            display.addChild(nbm);
            nbm.y = i * 50;
            notes.push(nbm); // by adding the display objects to an array you can reference them later

then at some later point in the Notes class you can reference your bitmaps by looping through the notes array

// initialize variables outside the loop
var localPos:Point 
var globalPos:Point;
var bounds:Rectangle;
var n:DisplayObject;
var stage:Stage = Main.StageRef;

for (var i:int = 0; i < notes.length; i++)
{
    trace( 'note' + i );
    n = notes[i] as DisplayObject; // cast during assignment since array items have ambiguous type

    // getBounds() returns a Rectangle representing the bounding box of the display object
    bounds = n.getBounds(stage);
    trace(
        'getBounds:',
        bounds.top, bounds.left, bounds.bottom, bounds.right
    );

    // localToGlobal() returns a Point which is just the position
    localPos = new Point( n.x, n.y );
    globalPos = display.localToGlobal(localPos)
    trace(
        'localToGlobal:',
        globalPos.x, globalPos.y
    );
}

A few points:

  1. I refactored some names to make the intention more clear
  2. I wonder why your Notes class extends Sprite, since it adds display (what you called "noteholder") to the stage. Assuming that's correct just make Notes a generic object i.e. doesn't seem to need to extend another class.
Mark Fox
  • 8,694
  • 9
  • 53
  • 75