A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: String returned from function

  1. #1
    Member
    Join Date
    Dec 2009
    Posts
    84

    String returned from function

    This is probably an easy question, and I am sure I am just forgetting the answer, but this is driving me NUTS! I have a button that triggers a function that returns a string. How do I trace the returned string?

    Actionscript Code:
    var user:String = new String("Hello World");
    function getUser(ev:Event):String{
        return(user);
    }
    hit_btn.addEventListener(MouseEvent.CLICK, getUser);

    BTW, I know I could just do "trace(user);", but the real problem I am working on is getting a value to one swf from it's parent swf via "dispatchEvent(new Event('someEvent'));" and using the returned string. Same principle here. Thanks.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The return value is ignored. The code that calls the event listener is not expecting a return value, so it just doesn't do anything with it.

    If you want to do something with user, you have to do something with user. You could call a method in some other object and pass it user, or you could create a new event type with a property for the user and dispatch that, or many other ways.

  3. #3
    Member
    Join Date
    Dec 2009
    Posts
    84
    Well, I am glad it is not something easy that I forgot, but I am still a little stumped. If you could advise I would really appreciate it. To help you undestand exactly what I am doing here, I will get a little more specific.

    I have a main swf called application. It lanches a child swf name maintenance like so:

    Actionscript Code:
    var maintReq:URLRequest = new URLRequest("maintenance.swf");
    var maintLdr:Loader = new Loader();
    maintLdr.load(maintReq);
    maintLdr.addEventListener('getUser', getUserName);
    maintLdr.addEventListener('closeMe', closeMaintenance);
    container.addChild(maintLdr);

    Then in the maintenance swf, I was hoping to have something like this to get the username from the parent swf (application).

    Actionscript Code:
    dispatchEvent(new Event('getUser'));

    This, does not seem like it is going to work. I thought about using a URLVariable, but I am not sure how to access that from the maintenance swf or if it is even possible.

    Any suggestions?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    URLVariables are not appropriate for this.

    The solutions I outlined above still fit. You can create an Event subclass which has your username in it:
    Code:
    public class UserEvent extends Event {
      public var userName:String;
      
      public UserEvent(type:String, uname:String, bubbles:Boolean = false, cancelable:Boolean = false){
         super(type, bubbles, cancelable);
         userName = uname;
      }
    
      //implement clone and tostring here, but I'm too lazy to do it right now.
    }
    And you can dispatch that UserEvent instead of a plain event
    Code:
    dispatchEvent(new UserEvent('getUser', user, true)); //you need bubbling
    And then in your listener, you'll cast to UserEvent and then pull out the userName property.

    Since you are dealing with two swfs, if you do it this way you have to be sure that you load the second swf into the same ApplicationDomain as the first so that you can use the same UserEvent class in both.

    Another way is to simply make userName public in the dispatching instance, and pull it out in the listener.

    Code:
    function getUserName(e:Event):void{
      var target:MovieClip = MovieClip(e.target);
      trace(target.user);
    }
    I'm not so much a fan of that since it relies on dynamic properties rather than strongly typed classes, but it's probably easier to implement.

  5. #5
    Member
    Join Date
    Dec 2009
    Posts
    84
    Ok, this is my first time creating a custom event and I have SEVERAL quesions about the big words that you just used. First of all, I have created tons of custom classes, but was completely unaware that you could create custom events all willy-nilly (which by the way is awesome!!). I am going to try to find a tutorial or something on creating a custom event that will familiarize me with the process rather than asking you a bunch of questions that will inevitably lower your AS3 IQ a few points.

    If you can suggest a tutorial, that would be awesome. But for now, I will just stick a pin in this problem until I become a little more profficient at creating custom events. After that, if I still have questions, I will be sure to post them here. Thanks for blowing my mind!

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm actually not sure if I understood you right the first time. Are you trying to send the user name from the loading to the loaded, or the loaded to the loading? The stuff I wrote was for the latter.

    On rereading, it seems you are trying to get the username from the loading instance into the loaded instance. My previous suggestions don't directly apply.

    What I'd probably do is have a public method in maintenance called setUser which takes the user name and sets it.

    Code:
    public function setUser(userName:String):void{
      this.userName = userName;
    }
    And then you can call that from the main swf in an event listener for the completed load operation.

    Or if you still want to use the getUser event model (if you want to set it more than once, or not on load), then you can have main call setUser in its getUserName method (though, I'd change the name).

    Code:
    function askForUserName(e:Event):void{
      var maintenance:MovieClip = MovieClip(e.target);
      maintenance.setUser(user);
    }
    Use a more specific type than MovieClip if you have one.
    Last edited by 5TonsOfFlax; 01-28-2011 at 06:53 PM.

  7. #7
    Member
    Join Date
    Dec 2009
    Posts
    84
    Application.swf launches login.swf. login.swf then passes the username (if name and password are valid) to application.swf into the variable "user". In application.swf there is a maintenance button that launches maintenance.swf. I need to somehow get the variable "user" (string) from application.swf to maintenance.swf. Parent to child passing seems a lot harder than child to parent passing.

    I just thought of something. Application.swf is sitting on an html page. Is there a way of storing the username in the html page so that it can be accessed by all launched swfs? Or would that create all kinds of problems and difficulties? This will potentially be used by hundreds (hopefully hundreds of thousands, but that is wishful thinking) of people at once. Would storing the username in the html cause them all to be treated as the same user? Just thinking out loud at this point.

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You can communicate between Objects or swfs in this case by the Document classes of the movies extending the same Abstract class.

    http://flashscript.biz/flblogger/?p=10
    - The right of the People to create Flash movies shall not be infringed. -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center