1
votes

I've got a block of code that's doing what I want - it generates a grid of MC's.

As soon as I put something like function blah() around it, it starts generating errors indicating lines of code I don't have e.g.

TypeError: Error #1010: A term is undefined and has no properties.
at flightCellMaker_fla::MainTimeline/myXMLtrace() [flightCellMaker_fla.MainTimeline::frame1:87]
at flightCellMaker_fla::MainTimeline/processFPBxml() [flightCellMaker_fla.MainTimeline::frame1:52]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

When I take the function out, it does what I want it to do. What's up with that?

var testXML:XML;
var myFPBxml:XML;

// Initialise a URLLoader to get XML data from XML file
var myFPBLoader:URLLoader = new URLLoader();
myFPBLoader.load(new URLRequest("flightPlannerBoard.xml"));


 // Check XML data fully loaded
 myFPBLoader.addEventListener(Event.COMPLETE, processFPBxml);


// Once the flight board planning data is loaded, save it to a variable
function processFPBxml(e:Event):void {
myFPBxml = XML(e.target.data);
myXMLtrace();
 }  


// Grab the XML data load completed and make it available elsewhere
function myXMLtrace(){
testXML = XML(myFPBxml);
}
trace("***********************" + testXML.*); This throws an error (not within myXMLtrace tho)

OK, so here's the rest of the code that will run correctly on it's own but not in a function:

// Create and place all the flight cells for planning and drag and drop 

// Setup 2 loops: j for columns and i for Rows
for (var j:Number =0; j < rowNum; j++){

for (var i:Number =0; i<9; i++){

    // Create copies of flightCell for board grid
    var my_mc = new flightCell();
    my_mc.name = "mc"+i+j;

    addChild(my_mc);

    // Set event Listeners on all Child objects
    my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler);
    my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_OUT, fl_MouseOutHandler);
    my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_DOWN, fl_MouseDownHandler);
    my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_UP, fl_MouseUpHandler);

    Object(this).my_mc.yellowHiLite.visible = false;


    //cellPos[j] = myXML.cellPosX[j];
    //trace(stage.myXML.*);

    //trace(my_mc.name);
        my_mc.x = (100 + colWidth);
        my_mc.y = myRowHeight;

    colWidth = colWidth + 155;

    //trace(myXML.*);

    cellArray[arrayCount] = [my_mc.x, my_mc.y];
    trace("CellArrayCount = " + cellArray[arrayCount]);
    arrayCount = arrayCount + 1;
}

myRowHeight = myRowHeight + 105;
colWidth = 50;
}
3
var testXML:XML; var myFPBxml:XML; // Initialise a URLLoader to get XML var myFPBLoader:URLLoader = new URLLoader(); myFPBLoader.load(new URLRequest("flightPlannerBoard.xml")); // Check XML data loaded myFPBLoader.addEventListener(Event.COMPLETE, processFPBxml); // Save it to a variable function processFPBxml(e:Event):void { myFPBxml = new XML(e.target.data); myXMLtrace(); } // Grab the XML data load completed and make it available elsewhere function myXMLtrace(){ testXML = new XML(myFPBxml); trace("***********************" + testXML.*); } - user1203605
Not in a comment :D It's unreadable. Edit your question. - Jonatan Hedborg
Sorry - new to all this! - user1203605

3 Answers

2
votes

Your trace is throwing an error because it is being executed before the XML is loaded.

ActionScript is asyncronous, which means that while the XML is being loaded, the program execution carries on going, and looks something like this:

  1. Declare testXML:XML and myFPBxml:XML
  2. Create loader and start loading
  3. Add a listener for load completion
  4. Trace testXML
  5. Execute processFPBxml when the XML is loaded

If you move your trace into the myXMLTrace function then it will work correctly.


With the newly added code, the problem is with this line:

Object(this).my_mc.yellowHiLite.visible = false;

There is no need to use the this keyword at all here. This will work inside or outside a function:

my_mc.yellowHiLite.visible = false;

The reason your code fails inside a function is because inside and outside the function this will be the scope of the object in which this code resides. However, when you put the code into a function, the reference you create - my_mc - is scoped locally to the function, not to the parent object, and so this.my_mc is undefined, because this is not the function scope.

0
votes

Are you taking the code out of the function it's currently in and calling the new function from where the code used to be? Or are you trying to define a function variable? If you're only putting function blah() { ... } around code in the same place it is currently running, you'll get a syntax error for sure. In that case, try:

var blah:Function= function() { ... };
blah();
0
votes

If you declare variables within a function they fall inside the scope of the function only.

It sounds like you are trying to access variables from elsewhere in your code.

So what you need to do is declare your variables outside of the function. e.g.

var one:VarType;
var two:VarType;

function blah():void {
 trace(one);
}