Empty variable after switching timeline or MC

Hi,

I’m having difficulty accessing a variable value within nested MCs.

On Scene 1 in the addFullPanel function, the variable panelNames exists.
However, I’d like to use this variable value within the MC FullProjectPanel in the switch function in order to load a corresponding xml file.

As it stands the variable is empty within the FullProjectPanel function.

Hope this makes sense.:blush:

Cheers

Don’t declare the variable within a function scope, make it a global variable.

Hi EastCoast

First of all thanks for your response.

I declared panelNames (or panelNumber) outside the function, but the script is throwing the error that panelNumber is not a number.

My function addFullPanel has only panelNames:String as an argument. Do I need to supply the panelNumber:Number as an argument too to the addFullPanel?

Not sure what’s going on, but I strongly believe the value of the variable is not carried forward into the nested MC FullProjectPanel because they’re nested and on a different timeline.

Since the CS3 fla file comprises of nested MCs I’ve attached it as a zip file for ease of reading.

Many thanks in advance.

Regards

I’ve defined the variable panelNumber as a global variable in the file global.as, which reads:


package 
{
    public class global
    {
        public static var data:Object = {};
        public static var panelNumber1:int = 1;
        public static var panelNumber2:int = 2;
    }
}

The AS on the main timeline (Scene 1) reads:


import com.greensock.TweenLite;

var fullProjectPanel: FullProjectPanel;

function loadProgress(event:ProgressEvent):void {

    var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
    percentLoaded = Math.round(percentLoaded * 100);
    trace("Loading: "+percentLoaded+"%");
}

function loadComplete(event:Event):void {

    trace("Complete");
}

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

// ADD FULL PROJECT PANEL ( runs when clicking on project panel(2nd frame of mainContainer, line 28))

//var panelNumber:Number;

function addFullPanel(panelNames:String) {

    trace("panelNames (inside addFullPanel) = "+panelNames);
    var path:String = "image/"+panelNames;
    fullProjectPanel = new FullProjectPanel();
    var request:URLRequest = new URLRequest(path);
    loader.load(request);
    trace("path (inside addFullPanel) = "+path);

    TweenLite.from(fullProjectPanel, 0.6, {alpha:0, delay:0.5});
    addChild(fullProjectPanel);

    // indicate state
    fullProjectPanelUp = true;

    // listen for click on close button
    fullProjectPanel.closePanel.addEventListener(MouseEvent.CLICK, onCloseClick);
    fullProjectPanel.closePanel.buttonMode = true;
}

function onCloseClick(evt:MouseEvent):void {

    // run the slide up function that slides up the project container 
    mainContainer.slideUp();

    // remove the panel
    removeChild(fullProjectPanel);

    // delay the state change to allow for transition
    TweenLite.delayedCall(0.4, upFalse);
}

function upFalse():void {
    
    fullProjectPanelUp = false;
}

And the AS in the mainContainer MC which is also situated on the main timeline (Scene 1) is:


import com.greensock.TweenLite;
import com.greensock.easing.Back;
import com.greensock.loading.*;
import com.greensock.loading.display.*;
import com.greensock.events.LoaderEvent;

import flash.display.Sprite;
import flash.text.*;
import flash.display.BitmapData;
import flash.display.Bitmap;

// CONTAINER FOR SCROLLING
var panelContainer:Sprite = new Sprite;
addChild(panelContainer);

// INITIAL VARIABLES

var imageArray:Array = new Array();
var currentButton:Object = new Object;
var selectedSection:Number = 0;
var myThumb:Array = new Array();
var myImage:Array = new Array();

function progressHandler(event:LoaderEvent):void {
    trace("progress: " + event.target.progress);
}

function loadProgress(event:ProgressEvent):void {  
    var percentLoaded:Number = Math.ceil(event.bytesLoaded/event.bytesTotal); 
    trace("Loading: "+percentLoaded+"%");  
}  

var xmlLoader:URLLoader = new URLLoader();
var xmlData: XML;

//Load the XML file
xmlLoader.load(new URLRequest("xml/slides2.xml"));

//Adding an event listener to notify when loading is completed
xmlLoader.addEventListener(Event.COMPLETE,ParseXML);

function ParseXML(event:Event):void {

    var panelArray:Array = new Array();
    xmlData = new XML(event.target.data);
    var lengthDoc = xmlData.image.name.length();

    for (var j:int=0; j < lengthDoc; j++) {

        var xmlLoader = new Loader();
        var panelItem:ProjectPanel = new ProjectPanel;
        var imageName = (xmlData.image[j].name);
        var imageNumber:Number = (xmlData.image[j].name);
        panelItem.name = imageName;

        panelItem.addEventListener(MouseEvent.CLICK, onClick);
        panelItem.buttonMode = true;
        panelItem.project_title.text = (xmlData.image[j].description.@title);
        panelItem.project_description.text = (xmlData.image[j].description);

        var imageThumb:String = xmlData.image[j].path.@thumb.toString();
        var imageLarge:String = xmlData.image[j].path.@large.toString();

        myThumb[j] = imageThumb;
        myImage[j] = imageLarge;

        var imageUrlRequest:URLRequest = new URLRequest(myThumb[j]);
        imageArray[j] = xmlLoader.load(imageUrlRequest);
        imageArray.push(xmlLoader);
        panelItem.project_image.MovieClip = (imageArray[j+1]);
        panelItem.project_image.addChild(imageArray[j+1]);

        if (j > 0) {

            panelItem.x = j*(panelArray[j-1].width+10);
        }
        panelArray.push(panelItem);
        // adding Items as children to Container
        panelContainer.addChild(panelItem);
    }
}

function errorHandler(event:LoaderEvent):void {
    trace("error occured with " + event.target + ": " + event.text);
}

function onClick(evt:MouseEvent) {

    // tween all panels below visible area when clicking on projectPanel
    TweenLite.to(panelContainer, 0.5, {y:stage.stageHeight+250, ease:Back.easeIn});

    var target:MovieClip = evt.currentTarget as MovieClip;
    var panelNames:String = target.name;
    
    // running function on the main timeline
    MovieClip(parent).addFullPanel(panelNames);
}

// slide the panels back up on closing the single panel project
function slideUp():void {
    
    TweenLite.to(panelContainer, 0.5, {y:0, ease:Back.easeOut});
}

// HORIZONTAL SCROLLING 
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);

function onMove(evt:MouseEvent):void {

    // scroll only if fullprojectpanelup is false to prevent from interfering with transitions
    if (MovieClip(this.parent).fullProjectPanelUp==false) {

        TweenLite.to(panelContainer, 0.3, {x:-(stage.mouseX/980)*panelContainer.width+stage.stageWidth/2});
    }
}

stop();

And I import the variable panelNumber in the FullProjectPanel MC which is situated directly on the main timeline (Scene 1):


stop();

import global;

global.data.panelNumber1;
global.data.panelNumber2;

var galleryData:XML;

var galleryLoader:URLLoader = new URLLoader();
galleryLoader.addEventListener(Event.COMPLETE, onGalleryDataLoaded, false, 0, true);

switchPanel(global.panelNumber1, global.panelNumber2);

function switchPanel(panelNumber1, panelNumber2) {

switch(panelNumber1, panelNumber2) {

    case 1:
    trace("panel chosen (inside switch landscapes) = "+global.panelNumber1);
    galleryLoader.load(new URLRequest("xml/landscapes_gallery.xml"));
    break;

    case 2:
    trace("panel chosen (inside switch k9s) = "+global.panelNumber2);
    galleryLoader.load(new URLRequest("xml/k9_gallery.xml"));
    break;
    }
}

function onGalleryDataLoaded(evt:Event):void {

    galleryData = new XML(evt.target.data);
    // start checking if the movie is loaded
    addEventListener(Event.ENTER_FRAME, onCheckLoaded, false, 0, true);
    galleryLoader.removeEventListener(Event.COMPLETE, onGalleryDataLoaded);
}

function onCheckLoaded(evt:Event):void {

    if (root.loaderInfo.bytesTotal == root.loaderInfo.bytesLoaded) {

        gotoAndStop("main");
        removeEventListener(Event.ENTER_FRAME, onCheckLoaded);
    }
}

This time it will load the xml file from within the switch function, however, it’ll only select case 2 in the switch function.
I hope someone can tell me how can I correct this?

Cheers