6 Replies - 949 Views - Last Post: 02 July 2011 - 06:48 AM

#1 flasher333   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-June 11

Empty variable after switching timeline or MC

Posted 21 June 2011 - 09:39 AM

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. :online2long:

Cheers

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Empty variable after switching timeline or MC

#2 chinchang   User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 727
  • Joined: 22-December 08

Re: Empty variable after switching timeline or MC

Posted 21 June 2011 - 11:28 AM

Any variable defined in one function cannot be accessed anywhere outside that class. So if you want to access the variable panelNames outside the function, declare it as global variable (outside all functions). Now your FullProjectPanel objects can access it.
Was This Post Helpful? 0
  • +
  • -

#3 flasher333   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-June 11

Re: Empty variable after switching timeline or MC

Posted 21 June 2011 - 02:56 PM

View Postchinchang, on 21 June 2011 - 11:28 AM, said:

Any variable defined in one function cannot be accessed anywhere outside that class. So if you want to access the variable panelNames outside the function, declare it as global variable (outside all functions). Now your FullProjectPanel objects can access it.


Hi Chinchang,

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 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.

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

Attached File(s)


This post has been edited by flasher333: 21 June 2011 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 flasher333   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-June 11

Re: Empty variable after switching timeline or MC

Posted 22 June 2011 - 07:05 AM

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][email protected]);
        panelItem.project_description.text = (xmlData.image[j].description);

        var imageThumb:String = xmlData.image[j][email protected]();
        var imageLarge:String = xmlData.image[j][email protected]();

        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

This post has been edited by flasher333: 22 June 2011 - 07:12 AM

Was This Post Helpful? 1
  • +
  • -

#5 chinchang   User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 727
  • Joined: 22-December 08

Re: Empty variable after switching timeline or MC

Posted 22 June 2011 - 01:03 PM

What are you trying to do with this function :

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;
    }
}



The switch statement is wrongly used here. switch take a variable which is compared with the cases. You on the other hand have provided 2 parameters which makes no sense.
If you want to load xml files based on panelNumber, there should be just one variable storing the panel number. Why 2 variables ?
Was This Post Helpful? 0
  • +
  • -

#6 flasher333   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-June 11

Re: Empty variable after switching timeline or MC

Posted 01 July 2011 - 07:34 AM

Hi Chin Chang

Thanks again for your response.
Yep, you're right, the switch function is not correct.

However, with just one variable I'm still getting a populated variable in the addFullPanel function as it should, but an empty variable in the FullProjectPanel MC.

That is, when I trace:

panelNames (inside addFullPanel) = 1.
Panel Number (inside onclick) = 1.
Panel Number (in FullProjectPanel MC) I get: 1120: Access of undefined property panelNumber.

I've tried defining panelNumber (or panelName) as a global variable, but that wouldn't work either. I'm not sure how to go from here.

Please find attached my fla file.

Many thanks in advance.

Cheers

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#7 flasher333   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 03-June 11

Re: Empty variable after switching timeline or MC

Posted 02 July 2011 - 06:48 AM

okay, got it sorted now, utilizing global variables.
Will post solution shortly.

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1