Named functions with return values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebarry
    New Member
    • Jul 2007
    • 69

    Named functions with return values

    Hi,

    I would like to change the following anonymous to a named function which receives the newsID as an argument and returns the title, content and image of the article (which are currently done within the function).

    Code:
    titleMC.onRelease = function()
    {
        varSenderContent = new LoadVars();
        varReceiverContent = new LoadVars();
        varSenderContent.newsID = this.newsID;
        varSenderContent.sendAndLoad("localhost/GetNewsDetail.php", 
             varReceiverContent, "GET");
        varReceiverContent.onLoad = function() 
        {
             for (p in this) 
             {
                  if (typeof this[p] == "string") 
                  {
    	 articleContent = this.data.split('~');
    	 _root.copy_mc.article.text = articleContent[1];
    	 image = "http://www.bemod.co.uk/images/newsImages/" + 
                           articleContent[2];
    								var CTNR_WIDTH=525; //original width of container			var CTNR_HEIGHT=156; //original height of container
    								loadMovie(image, "_root.photo_mc"); 
    								_root.photo_mc._xscale=CTNR_WIDTH/800*100;							_root.photo_mc._yscale=CTNR_HEIGHT/600*100;
    								_root.title_mc.title.text = articleContent[0];
                }
            }
        }
    }
    For calling the function I think I use the following syntax, once it's been given a name,
    Code:
    titleMC.onRelease = returnArticleContent( newsID )
    but how do I retrieve the values that I returned from the function?

    Thanks,

    Sean
  • xNephilimx
    Recognized Expert New Member
    • Jun 2007
    • 213

    #2
    Hi, you cannot make a function return multiple values, what you can do is make the function do all by itself without returning anything, or returnan array or an object and use that returned array/object to do whatever you whant.
    Anyway, you cannot call a function that return values the way you did, that way the button will recieve the returned "something" . You need to call the function from a variable, so the variable will be the one recieving what the function returns. Like this:
    [CODE=actionscri pt]
    titleMC.onRelea se = function() {
    //if the function returns an array:
    var some:Array = returnArticleCo ntent; //yes, if you dont pass parameters, you don't put ();
    //if the function returns an object:
    var some:Object = returnArticleCo ntent;

    //now do whatever you want with the "some" variable.
    }
    [/CODE]
    If the function returns an object, the variable will become that object. And then you can work with that variable -now an object- and do whatever you want width it.

    Kind regards,
    The_Nephilim

    PD: Sorry if I missed any "k", I'm not in my house and this eyboard sucs

    Originally posted by Sebarry
    Hi,

    I would like to change the following anonymous to a named function which receives the newsID as an argument and returns the title, content and image of the article (which are currently done within the function).

    Code:
    titleMC.onRelease = function()
    {
        varSenderContent = new LoadVars();
        varReceiverContent = new LoadVars();
        varSenderContent.newsID = this.newsID;
        varSenderContent.sendAndLoad("localhost/GetNewsDetail.php", 
             varReceiverContent, "GET");
        varReceiverContent.onLoad = function() 
        {
             for (p in this) 
             {
                  if (typeof this[p] == "string") 
                  {
    	 articleContent = this.data.split('~');
    	 _root.copy_mc.article.text = articleContent[1];
    	 image = "http://www.bemod.co.uk/images/newsImages/" + 
                           articleContent[2];
    								var CTNR_WIDTH=525; //original width of container			var CTNR_HEIGHT=156; //original height of container
    								loadMovie(image, "_root.photo_mc"); 
    								_root.photo_mc._xscale=CTNR_WIDTH/800*100;							_root.photo_mc._yscale=CTNR_HEIGHT/600*100;
    								_root.title_mc.title.text = articleContent[0];
                }
            }
        }
    }
    For calling the function I think I use the following syntax, once it's been given a name,
    Code:
    titleMC.onRelease = returnArticleContent( newsID )
    but how do I retrieve the values that I returned from the function?

    Thanks,

    Sean

    Comment

    • Sebarry
      New Member
      • Jul 2007
      • 69

      #3
      Thanks so much. That's perfect. You star!!!!

      Originally posted by xNephilimx
      Hi, you cannot make a function return multiple values, what you can do is make the function do all by itself without returning anything, or returnan array or an object and use that returned array/object to do whatever you whant.
      Anyway, you cannot call a function that return values the way you did, that way the button will recieve the returned "something" . You need to call the function from a variable, so the variable will be the one recieving what the function returns. Like this:
      [CODE=actionscri pt]
      titleMC.onRelea se = function() {
      //if the function returns an array:
      var some:Array = returnArticleCo ntent; //yes, if you dont pass parameters, you don't put ();
      //if the function returns an object:
      var some:Object = returnArticleCo ntent;

      //now do whatever you want with the "some" variable.
      }
      [/CODE]
      If the function returns an object, the variable will become that object. And then you can work with that variable -now an object- and do whatever you want width it.

      Kind regards,
      The_Nephilim

      PD: Sorry if I missed any "k", I'm not in my house and this eyboard sucs

      Comment

      • xNephilimx
        Recognized Expert New Member
        • Jun 2007
        • 213

        #4
        You're welcome Sebarry.
        Good luck with your project.

        Best regards!
        The_Nephilim

        Originally posted by Sebarry
        Thanks so much. That's perfect. You star!!!!

        Comment

        Working...