Topic: how to define gloabl varible in actionscript2

hi all,
I created a class

class A
{
var data:String;

public function A()
{
  this.data = "something";   
}

public function getData()
{
  return this.data; // doesn't work
}


}

Re: how to define gloabl varible in actionscript2

So you tried:

import A;
trace((new A()).getData());

...and it didn't output "something"?

Re: how to define gloabl varible in actionscript2

Duane wrote:

So you tried:

import A;
trace((new A()).getData());

...and it didn't output "something"?


public function getData()
{
  return this.data; // doesn't work
}


I import it and Inside the function this.data is undefined

Re: how to define gloabl varible in actionscript2

Can you cut/paste the precise code you used to test the class?

5 (edited by riyadh047 2011-10-26 12:30:29)

Re: how to define gloabl varible in actionscript2

class A
{
var data:String;

public function A(path)
{
   var host = this;
   var receiver:LoadVars = new LoadVars();
   var sender:LoadVars = new LoadVars();
   sender.sendAndLoad(path, receiver, "POST");

  receiver.onData = function(src:String)
  {
    host.setData(src);
  };

  receiver.onLoad = function(success:Boolean)
  {
    .............
  };
}
public function setData(src:String)
{
  this.data = src;
}
public function getData(void):String
{
  return this.data;
}

}

import A;
class B
{
   static var b:B;
   var a:A;
  public function B()
  {
    this.a = new A(path);
   this.a.getData(); undefined
  }
 
  static function main(mc)
{
  b = new B();
}
}

Re: how to define gloabl varible in actionscript2

OK, it you're expecting that the getData() call immediately after the new A() in main() will have data - it won't.

The reason is that the sendAndLoad() call is asynchronous - it will take at least one frame time for the HTTP request to be sent and the data received and parsed.  Until then, this.data will be undefined.

7 (edited by riyadh047 2011-10-26 12:43:48)

Re: how to define gloabl varible in actionscript2

sorry i could not logged in for few hours, because of spam of my post. can you please see my code again, i called it from another class.but thanks for nice conversation.


class A
{
var data:String;

public function A(path)
{
   var host = this;
   var receiver:LoadVars = new LoadVars();
   var sender:LoadVars = new LoadVars();
   sender.sendAndLoad(path, receiver, "POST");

  receiver.onData = function(src:String)
  {
    host.setData(src);
  };

  receiver.onLoad = function(success:Boolean)
  {
    .............
  };
}
public function setData(src:String)
{
  this.data = src;
}
public function getData(void):String
{
  return this.data;
}

}

import A;
class B
{
   static var b:B;
   var a:A;
  public function B()
  {
    this.a = new A(path);
   this.a.getData(); undefined
  }
 
  static function main(mc)
{
  b = new B();
}
}

in new class B this.a.getData() is undefined

Re: how to define gloabl varible in actionscript2

It doesn't matter - you can't expect to call getData() immediately after creating the instance of A.

The call to sendAndLoad() returns immediately, but not with the data.  The data does not exist until the fetch succeeds and the onLoad handler is called.

Re: how to define gloabl varible in actionscript2

could you please give a suggestion that how can i do it. thanks

Re: how to define gloabl varible in actionscript2

Well, this is basic Flash stuff, not specifically related to the Chumby.

All HTTP calls (LoadVars.load, LoadVars.sendAndLoad(), XML.load(), and XML.sendAndLoad()) are all asynchronous.  You create an object, set up handlers to process the results of the fetch, then perform the call to perform fetch.  The actual fetch may actually happen at some arbitrary time in the future, and the results of the fetch may come in at some other time farther in the future.  What you do is set up the handlers so that whatever you want to do with the data is tied to the result handlers, not the fetch itself.

Try this - see how I call a method in class B when the data is actually received by class A, in the onData handler:

import B;
class A
{
    private var data: String;

    public function A(path:String,b:B)
    {
        var host:A = this;
        var receiver: LoadVars = new LoadVars();
        var sender: LoadVars = new LoadVars();
        receiver.onData = function(src: String)
        {
            host.setData(src);
            b.gotData();
        };
        sender.sendAndLoad(path, receiver, "POST");
    }

    public function setData(src: String):Void
    {
        this.data = src;
    }

    public function getData() : String
    {
        return this.data;
    }
}

import A;
class B
{
    static var b: B;
    private var a: A;

    public function B() {
        this.a = new A(path,this);
     }

    public function gotData():Void
    {
        trace(this.a.getData());
    }

    static function main(mc:MovieClip):Void
    {
        b = new B();
    }
}

Re: how to define gloabl varible in actionscript2

okkkkkkkkk thanksssssss, i will try with it.

Re: how to define gloabl varible in actionscript2

heyyyyyyyyy Duane, have a nice day, it's working, thanks dear

Re: how to define gloabl varible in actionscript2

class A
{
    private var data: String;

    public function A(path:String,b:B)
    {
        var host:A = this;
        var receiver: LoadVars = new LoadVars();
        var sender: LoadVars = new LoadVars();
        receiver.onData = function(src: String)
        {
            host.setData(src);
            b.gotData();
        };
        sender.sendAndLoad(path, receiver, "POST");
    }

    public function setData(src: String):Void
    {
        this.data = src;
    }

    public function getData() : String
    {
        return this.data;
    }
}

import A;
class B
{
    static var b: B;
    private var a: A;

    public function B() {
        this.a = new A(path,this);
     }

    public function gotData():Void
    {
        trace(this.a.getData());
    }

    static function main(mc:MovieClip):Void
    {
        b = new B();
    }
}


hi Duane, I couldn't  assign the value in a variable in class B, I have modified class B and created another class C

import A;
class B
{
    private var a: A;
    private var data1:String;

    public function B() {
        this.a = new A(path,this);
     }

    public function gotData():Void
    {
        this.data1 = this.a.getData() // assigning the data for accessing from other class but doesn't work
    }

    public function getData1():String
    {
      return this.data1;
    }

}


import B;

class C
{
  static var c:C;
  private var b:B;

  public function C()
  {
    this.b = new B();
    this.b.getData1();// doesn't work
  }
 
  static function main(mc:MovieClip)
  {
    c = new C();
  }
 
}

Re: how to define gloabl varible in actionscript2

You just recreated the original problem, just in a different way.

You can't initiate a network fetch and immediately get the data you want - you have to wait for it to arrive first.  Your code does not do that.

Re: how to define gloabl varible in actionscript2

thanks, but how can i wait for this, by creating progress bar.

Re: how to define gloabl varible in actionscript2

You can't create a progress bar for loading LoadVars or XML data, at least in AS2.  All you can do is create some kind of spinner - the only two things you know is when you initiated the fetch, and when the data came back (because onData or onLoad is called).  You get no feedback on loading progress or *when* it may complete in the future.

There are lots of free tutorials out there on how to load data into Flash - you might want to review some of them to see how it's done.

Re: how to define gloabl varible in actionscript2

thanks, I am  trying to do in another way, if you know any tutorial link please write.

Re: how to define gloabl varible in actionscript2

hey Duane, please give me an example, if you can!!!!!!!!!. thankssssssssssssss

Re: how to define gloabl varible in actionscript2

class A
{
    private var data: String;
    private var flag: Boolean;

    public function A(path:String)
    {
        var host:A = this;
        var receiver: LoadVars = new LoadVars();
        var sender: LoadVars = new LoadVars();
        receiver.onData = function(src: String)
        {
           if(src!=undefined)
           {
             if(receiver.getBytesLoaded() == receiver.getBytesTotal())
             {
               host.setData(src);
               receiver.onLoad(receiver.loaded);
             }
           }
           
        };

        receiver.onLoad = function(success:Boolean)
        {
          host.flag = success;
         };
        sender.sendAndLoad(path, receiver, "POST");
    }

    public function setData(src: String):Void
    {
        this.data = src;
    }

    public function getData() : String
    {
        return this.data;
    }
}

import A;
class B
{
    static var b: B;
    private var a: A;

    public function B() {
        this.a = new A(path,this);
        this.a.getData();// undefined
     }

    static function main(mc:MovieClip):Void
    {
        b = new B();
    }
}

i have check totalbytes is equal to loadedbytes and it's working, but still i didn't understand why data is not initialized, can you please correct it, if you know!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Re: how to define gloabl varible in actionscript2

Once again - you can't do this:

        this.a = new A(path,this);
        this.a.getData();// undefined

Re: how to define gloabl varible in actionscript2

okkkkkkkkkkk, but how !!!!!!!!!!!, you told me that to creat a spinner, it's a thread, but i have no idea how to create thread in actionscript2, there is a EventDispatcher by which we can create event, but i have to import the "import mx.events.EventDispatcher;", it's a package and i don't have the package but i am using linux and EventDispatcher is not exist in my system. please give me atleast one idea or example as you gave me before, it's important for me. I have spent lots of times.....................................

Re: how to define gloabl varible in actionscript2

My best recommendation is that you Google the term "loading data into Flash".  The first hit is a tutorial that looks pretty good, but there are many, many other links that can be helpful.

Re: how to define gloabl varible in actionscript2

thanks for your reply, all tutorials are related to xml, but how to access json data into onLoad event hadler

24 (edited by riyadh047 2011-11-09 04:23:08)

Re: how to define gloabl varible in actionscript2

hallo Duane, i couldn't solve the previous problem, i have another problem, could please see it
class DataHandlingClass
{
  static var dataHandlingClass:DataHandlingClass;
  var data:String = "{\"A\":\"abc\", \"B\":\"sfasd\",\"C\":\"erter\",\"D\":\"dfgfd\"}"; //json data format in string
   public function DataHandlingClass
   { 
     var send_xml:XML = new XML(data);
     var response_xml:XML = new XML();

     response_xml.onData = function(rawResponseData)
    {
      trace (rawResponseData); //wants to see the changes in the webservice
    };
   
    send_xml.sendAndLoad("url", response_xml, "POST");
   }

   static function(mc)
   {
     dataHandlingClass = new DataHandlingClass(); 
   }
}

I could not post it, can please tell me what's the wrong, i have tried with LoadVars, i could not, could you please give a suggestion. thanks

Re: how to define gloabl varible in actionscript2

You cannot POST JSON using LoadVars from Flash.  There apparently is a trick for doing this using XML objects, see this page.