views:

1160

answers:

1

I'm using Actionscript 2.0 in combination with PHP, now I can make a call to my PHP file and receive data but apparently I have to use that data immediately, I cannot use it to fill my class variables.

This is what I want :

class user {

var lastname:String;

function user(in_ID:Number){
  var ontvang:LoadVars = new LoadVars();
  var zend:LoadVars = new LoadVars();
  zend.ID = in_ID;
  zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
  ontvang.onLoad = function(success:Boolean) {
   if (success) {
    lastname = ontvang.lastname;
   } else {
    lastname = 'error';
   }
  };
}
}

I've found out that this is a big issue in AS2, I found this post to work around it if you're loading XML data but I can't seem to get it to work with LoadVars : http://www.actionscript.org/forums/showthread.php3?t=144046

Any help would be appreciated ..

+1  A: 

When your onLoad handler is called, it is being called as if it were a member function of the LoadVars instance, and not your user instance.

There are several ways around this, one is to use Delegate.create() to create a function which will work as intended, for example:

import mx.utils.Delegate;

class user {

    var lastname:String;
    var ontvang:LoadVars;

    function user(in_ID:Number){
                ontvang = new LoadVars();
                var zend:LoadVars = new LoadVars();
                zend.ID = in_ID;
                ontvang.onLoad = Delegate.create(this, onLoad);                
                zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");

                };
    }

    function onLoad(success:Boolean) : Void
    {
       if (success) {
          lastname = ontvang.lastname;
       } else {
          lastname = 'error';
       }
    }
}

Don't forget that the load is asynchronous - when you create one of your user objects, the member variables won't be immediately available. What you may need to do is let your user object be capable of signaling its readiness much like LoadVars does, (e.g. with a callback function provided by the caller) so that your app is driven by by these asynchronous events.

Paul Dixon
Okay 2 things :1. Where can I find the Delegate class ?2. In the onLoad function it now calls to ontvang.lastname but within the onLoad function there is no ontvang variable ..Thanks already though !
Pmarcoen
Ok apparently I just needed to add import mx.utils.Delegate;I tried using this.lastname instead of ontvang.lastnameIn my main file I now have var u = new user(1);tekst.text = u.getLastname();But I still get 'undefined' in my tekst textfield..
Pmarcoen
Sorry - have corrected it. ontvang needed to be a class member so that you could pick it up during onLoad.
Paul Dixon