dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Listening for 2 Events before triggering ONE function

  1. #1
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508

    Listening for 2 Events before triggering ONE function

    What is the best way to handle an object listening for 2 events before executing ONE function? (and I don't want the function to execute twice)

    In my case, I want a request to be made for some data to load at the same time that an exit animation is playing. Before I want my intro animation to occur, I need the data to load AND the exit animation to be done! Two separate events! I know I could make it so the data request is not made UNTIL the exit animation is done, but that seems wasteful. Why should an animation hold up a data request, they are mutually exclusive, but the intro animation depends on them both! please advise, thanks!

  2. #2
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Simple use the Event.COMPLETE to know when you data is loaded, once it's loaded you should add an ENTER FRAME event listener on the function that was occurring on COMPLETE and in that enter frame function you should have something like if(yourmc.currentFrame == yourmc.totalFrames) { // do something and remove the Enter Frame event listener } ;

    I'm quite sure that if you sit down and plan a bit you'll find a few more ways too but IMHO this is the most intuitive one ( for me at least ).
    Last edited by fx.barrett; 02-22-2008 at 07:19 PM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  3. #3
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    Using the enterframe or timer method, both events would need to add an enterframe and/or start a timer that checked for completion of both. And that does mean having a switch for each to report it's finished.
    Of course, there's an easier way with less overhead.
    Have both events trigger the same function, and have that function say

    doneLoadedFunction(evt:Event):void {
    completePhase++;
    completePhase>1 ? doSomething() : void;
    }

  4. #4
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    thanks for the suggestion...I guess I should have been more clear.

    My animation is not with a standard movieclip on the timeline. I'm using the Tweener class to do my animation. It has an "onComplete" handler to call a function after it's finished, but the other problem is, sometimes the animation could finish before the load, and other times, the load could complete before the animation (that would all depend on connection speed, etc.).

    Any other ideas?

  5. #5
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    ^ Yeah, triggering a function like that would be easier to understand and a bit more organized :P Good post Josh !



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  6. #6
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    joshstrike...that idea, similar to Plenary's, would work...but it seems there would be a less messy solution? I don't know...if I did, I would have done it already!

  7. #7
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Nopes, no more ideas, still the same thing I first thought about:

    Here's a fast mock-up:

    PHP Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.None;
    import fl.transitions.TweenEvent;

    var 
    moveX:Tween;
    var 
    bState:Boolean false;

    var 
    xmlLoader:URLLoader = new URLLoader();
    this.xmlLoader.load(new URLRequest("yourXML.xml"));

    this.xmlLoader.addEventListener(Event.COMPLETEloadComplete);

    this.moveX = new Tween(yourMovieClip"x"None.easeNone0100.5true);
    this.moveX.addEventListener(TweenEvent.MOTION_FINISHonAnimationFinish);

    function 
    loadComplete(event:Event):void
    {
        
    this.xmlLoader.removeEventListener(Event.COMPLETEloadComplete);
        
    stage.addEventListener(Event.ENTER_FRAMEtrackYourStuff);
    // end of loadComplete

    function onAnimationFinish(event:Event):void
    {
        
    this.bState true;
        
    this.moveX.removeEventListener(TweenEvent.MOTION_FINISHonAnimationFinish);
    // end of onAnimationFinish

    function trackYourStuff(event:Event):void
    {
        if (
    this.bState == true)
        {
            
    // do something
            
    stage.removeEventListener(Event.ENTER_FRAMEtrackYourStuff);
        }
    // end of trackYourStuff 
    Good luck.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  8. #8
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Been playing around with this a bit. I can't say that I found something more efficient but it might seem a tiny bit cleaner then with enter frame event listeners... Maybe it will give you some inspiration:

    It essentially does the same thing as an Enter Frame event listener would but a bit differently.

    PHP Code:
    var count:int 0;
    var 
    CUSTOM_EVENT:String "customEvent";

    this.box.addEventListener(MouseEvent.CLICKonClick);
    this.box2.addEventListener(MouseEvent.CLICKonClick);
    this.addEventListener(CUSTOM_EVENTonCustomEvent);

    function 
    onClick(event:MouseEvent):void
    {
        if(
    this.count != 2)
        {
            
    this.count++;
            
    this.dispatchEvent(new Event(CUSTOM_EVENT));
        }

    // end of onClick

    function onCustomEvent(event:Event):void
    {
        if(
    this.count == 2)
        {
            
    trace("Condition was met !!!");
            
    this.removeEventListener(CUSTOM_EVENT,onCustomEvent);
        }

    // end of onCustomEvent 
    PS: in the example I was using mouse interaction, the idea is the same with your functions. You should dispatch that custom even once your function finishes. If the COMPLETE finishes first, it will result in a "count = 1" ( same thing with the other one finishes ) and once the second function finishes it will increment count, resulting into count == 2 and that's when the custom event function triggers and does whatever you want it to do...
    Last edited by fx.barrett; 02-23-2008 at 11:26 PM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


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