views:

57

answers:

2

Hi All,

I have a flash movie that I need to read in a value from a PHP script in order to set which frame it starts from, I am using the following code:

if (loaded == total) {
        var lvContent = new LoadVars();
        lvContent.load("http://MY URL/Includes/getID.php");             
        trace("Who: " + lvContent.pageID);       
        lvContent.onLoad = function() {
            if (lvContent.pageID != "29") { //If it's the home page then play the full animation. If not .. don't.
                _root.gotoAndPlay(2);

            }else{
                _root.gotoAndPlay(90);
            }
        }      
    }

The problem is this is not working - it won't get into the load event. If I run the PHP manually I get "&pageID=29". If I debug this locally I get "Who: undefined" in the trace output window. From all the example I have read, I seem to be doing this correctly but it just doesn't seem to be working.

Flash: CS5 using Actionscript 2.0

Can someone take a look and let me know where I am going wrong please?

Thanks in advance.

+3  A: 

You are defining the onLoad event after loading the contents. I don't think that can work.

Try

if (loaded == total) {
        var lvContent = new LoadVars();

        lvContent.onLoad = function() {
            if (lvContent.pageID != "29") { 
        //If it's the home page then play the full animation. If not .. don't.
                _root.gotoAndPlay(2);

            }else{
                _root.gotoAndPlay(90);
            }
        }    
        lvContent.load("http://MY URL/Includes/getID.php");             
        trace("Who: " + lvContent.pageID);        
    }
Pekka
@Pekka `load()` is asynchronous, so the order may not be the issue here - but calling load after assigning `onLoad` is preferred nevertheless. He should be tracing pageID from the onload handler; that too after making sure that the load was success.
Amarghosh
Good point, wasn't aware flash runs things on a procedural basis. This has now got the onLoad event firing. The variable still came back as undefined but when I started using this. instead of lvContent. inside the onLoad, that sorted my issue. Many thanks, I have marked this as the answer as it got me on the right tracks.
webnoob
@Amarghosh Thanks for the advice, have implemented some checking as well in the onLoad, just to be safe.
webnoob
+3  A: 

You're tracing pageID before it is loaded; try this

var lvContent = new LoadVars();
lvContent.onLoad = function(success:Boolean) {
  if(!success) {
    trace("Failed to load");
    return;
  }
  trace("Who: " + lvContent.pageID);//trace from the onLoad handler
  if (lvContent.pageID != "29") 
    _root.gotoAndPlay(2);
  else
    _root.gotoAndPlay(90);
}
lvContent.load("http://MY URL/Includes/getID.php");
Amarghosh
Thanks for that, have implemented the success stuff in now. This is what you get when you copy and paste code, trace's in the wrong place and dodgy code :)
webnoob
+1 tracing `pageID` inside the onLoad handler is the correct way, I overlooked that.
Pekka