Skip to main content
October 5, 2011
Answered

Adding movies dynamically. gone bad.. color me stupid!

  • October 5, 2011
  • 1 reply
  • 997 views

Ls,

0: Its not rocket science, but my surname aint Oppenheimer either so... read on if ou dare.

1: this is my third attempt at mastering OO and flash CS5 so, i hope we can all have a laugh after.

2: In short.. I added a 64x64 tiled movie in flash. Read it out in an array, in a.. well original way(just to see if i could get it to work so stop lauging already), and tried to recreate them using a flash class.

2a: The recreate class works but shows the Tiles in a random order, every time a new one?!

this is what happens..

2b:     The nested , to be copied into the array, clips show up in the loop thru the DisplayContainer trace.

          I traced the x and y values of the stored clips. they are as in the original MC in flash.

          Adding them back to a new MC, i traced the values again.. they are the same as the trace in the store in the array.

2?: so i stored the display objects in an array. Recreated them on screen thru a class. Traced values match.

     on screen, they seem to be in random order.

X: I have included two screenshots, 1 of the original MC in flash and 1 of the resulting rebuild (just enjoy those and try to imagine how totally frustrated i am

)

conclusion: AAAAAAAAAAAAAA!

Please help and I will be eternally gratefull

I have included some code in the hope that it is enough to help you help me figure this out.

Flash file contains (all exported for actionscript):

MC conmtaining all the nested movieclips i want is called(instance name) LEVELA1, 

MC im extracting and trying to recreate is called : hit_bush_1 , this is not an instance name but a movieclip name

(and some more but i wont bore you with that).

and some actionscript that reads the DisplayObjectContainer object into an Array called Hit_o

embedded as in .fla frame1

import flash.display.DisplayObjectContainer;

import flash.display.*;


var bgdo:DisplayObject;
var hit_o:Array = [];
var hit_i:int = 0;

var currentlvl:String = "";


function getlevelvars(container:DisplayObjectContainer, slvl:String):void
{
    var child:DisplayObject;

    for (var i:uint=0; i < container.numChildren; i++)
    {
 
    child = container.getChildAt(i);

// Only looking for the movieclips after the word LEVEL


  if ( child.name.substr(0,3) == "LEV"){
   currentlvl = child.name;
   trace("level found", currentlvl, slvl);
  }
  

// if the Mc encountered is the same as the one requested, start pushing them into the array.

 
  if (currentlvl == slvl){

   if ( child.toString().substr(8,1) == "h"){
                                                 trace ("hitfound:" + child, child.x, child.y);
   hit_o[hit_i] = child;
   hit_i++;
   }
  }

// recurse into the displayobjectcontaner

    if (container.getChildAt(i) is DisplayObjectContainer)
        {
            getlevelvars(DisplayObjectContainer(child), slvl)
        }

    }

}

// getting flash MC into array

// entering the DisplayObject list from the MC LVL_ALL, looking for the MC's nested under LEVELA1

// The movieclip has instance name LEVELA1

// after this the array hit_o is supposed to contain all the display objects from the MC that are named hit_bush_1

// the integer hit_i is the number of nested movieclips

var scont:DisplayObjectContainer;

scont = this.getChildByName("LVL_ALL").parent;

getlevelvars(scont,"LEVELA1");  

// I pass the array to the recreating class (see below)

var mygameht:gameht = new gameht(hit_o, hit_i);

addChild(mygameht);

.as classes

gameht.as (rebuilds the movieclip from the array) see below:

package  {
import flash.display.MovieClip;
import flash.display.DisplayObject;

public class gameht extends MovieClip {

  public function gameht(ho:Array,hc:int) {
   // constructor code
  
   this.name = "myHt";
   this.x = 0;
   this.y = 0;
   this.visible=true;
  
    this.populateht(ho,hc);
  }
 
function populateht (ho:Array,hc:int): void {
   var ts:String;
   var mdo:DisplayObject;
  
   mdo = ho[0];
     
   ts = mdo.toString().substring(8);
   ts = ts.substr(0, (ts.length -1));
  
   //trace(ts);
  
   for (var to:int=0; to < hc; to++){
    mdo = ho[to];
   
    if (ts == "hit_bush_1")
      var tile:MovieClip = new hit_bush_1();

     tile.x = mdo.x
     tile.y = mdo.y
     tile.name = mdo.toString() + mdo.toString();
     tile.visible= true;
    
     addChild(tile);
     trace("adding: ", tile.x, tile.y, tile, tile.name)
    }
   }
  
 

}

}

This topic has been closed for replies.
Correct answer

It should work. I suspect that your reliance on naming conventions is what makes it do wrong things. In other words, make sure that names of original instances on stage are corresponding with what you are reading when you collect them and when you instantiate hit_bush_1 in populateht.


Andrei belay that,

I discovered that it was something internally wierd with the library mc.

Replaceing the vector graphics with a bitmap gave the same strange results.

Deleting it and creating a new one works fine.

Curse you flash cs5! =))

Anyway.. bolsjoi spasiba

1 reply

Inspiring
October 5, 2011

What is the problem?

October 5, 2011

Hello Andrei,

The problem is:

As you can see from the screenshots, the locations of the green movieclips created in the gameht.as class do not correspond to their original positions i passed to that class. Picture 1 is an flash stage object. Picture 2 is a dynamic movie created by the Class.

Tracing shows the exact coords from pic 1 on storing them in the array and on adding them to the movieclip.

The result, as you can see in pic 2, is that the mc's have very different positions from the original.

regards,

Mac

Inspiring
October 6, 2011

It should work. I suspect that your reliance on naming conventions is what makes it do wrong things. In other words, make sure that names of original instances on stage are corresponding with what you are reading when you collect them and when you instantiate hit_bush_1 in populateht.