Felipe - 

////////////////


call (Function.call method) uses comma separated values; 

  currentScope.func.call ( alternateScope, arg1, arg2, arg3 ) 

apply (Function.apply method) uses an array of elements; 

  var args = [arg1, arg2, arg3];
  currentScope.func.apply (alternateScope, args );


////////////////


apply works well with the 'arguments' of a function because 'arguments' is
already in array form. 

  function myFunc(){ trace( 'you passed: ' + arguments ) };
  myFunc ( 1, 2, 3, 4, 5, 6 ) // you passed: 1, 2, 3, 4, 5, 6  


////////////////


To simplify the process of using "" and eval(), you could use the create
method in the Delegate class found at mx.utils.Delegate.

  var callback:Function = mx.utils.Delegate.create ( _root, nextFrame );
  anyOtherObject.func = function(){ callback () };
  anyOtherObject.func ();

Now whenever/wherever callback is called, nextFrame runs from the scope of
_root;


////////////////


Beyond this current issue, if you're interested in more information on the
Delegate and a list of several spin-offs, I've put together a list at
http://www.justgooddesign.com/blog/jgdelegate.htm


////////////////


So how about a few different approaches? 2&3 have a Delegate style built-in.


////////////////


/////
/////  1. simple
/////




function rew ( mc:MovieClip, func:Function ) 
{
  mc.onEnterFrame = function(){
    if ( mc._currentframe != 1 ) mc.gotoAndStop ( mc._currentframe - 1 );
    if ( mc._currentframe == 1 ) {
      mc.stop();
      delete mc.onEnterFrame
      if ( func!=undefined ) func();
    }
  }
}
_root.rew( d.mask, mx.utils.Delegate.create ( _root, nextFrame ) );




/////
/////  2. built in proxy
/////




function rew ( mc:MovieClip, scope, func:Function ) 
{
  var args:Array = arguments.slice( 3, arguments.length); 
  mc.onEnterFrame = function(){
    if ( mc._currentframe != 1 ) mc.gotoAndStop ( mc._currentframe - 1 );
    if ( mc._currentframe == 1 ) {
      mc.stop();
      delete mc.onEnterFrame
      if ( func!=undefined ) func.apply( scope, args);
    }
  }
  mc.onEnterFrame(); // fire immediately - or remove to wait for next frame
}

//examples
_root.rew( d.mask, _root, _root.nextFrame );
_root.rew( d.mask, _root, trace, 'animationDone: ' + this );




/////
/////  3. complex - with proxy and scope/func check
/////




function rew ( mc:MovieClip, scope, func:Function ) 
{
  var inx = 3;
  if ( typeof ( scope ) == "function" ) { 
    func = scope; scope = this; inx = 2;
  }
  var args:Array = arguments.slice( inx, arguments.length);             
  mc.onEnterFrame = function(){
    if ( mc._currentframe != 1 ) mc.gotoAndStop ( mc._currentframe - 1 );
    if ( mc._currentframe == 1 ) {
      delete mc.onEnterFrame
      if ( func!=undefined ) func.apply( scope, args);
    }
  }
  mc.onEnterFrame(); // fire immediately - remove to wait for next frame
}

//examples
_root.rew( d.mask, nextFrame );
_root.rew( d.mask, _root, _root.nextFrame );
_root.rew( d.mask, _root, trace, 'animationDone: ' + this );




_____________________________

Jesse Graupmann
www.jessegraupmann.com
www.justgooddesign.com/blog/
_____________________________



-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Felipe
Hefler
Sent: Sunday, February 18, 2007 11:39 AM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Passing a Function as Parameter and call
functionlater

I've tried it, and didn't work. Can you say how this reference should be?

On 18/02/07, Wagner Amaral <[EMAIL PROTECTED]> wrote:
>
> I'm not testing, but you could use:
>
> f.call();
>
> as long as f is a reference to a Function object
>
>
>
> On 2/18/07, Felipe Hefler <[EMAIL PROTECTED]> wrote:
> >
> > Thank you dr.ache.
> >
> > Wasn't that  the problem. Anyway your answer helped me a lot. See what
> > solution I came up:
> >
> > function rew(m:MovieClip, f:Function) {
> >     m.onEnterFrame = function() {
> >         var cf:Number = m._currentframe;
> >         var tf:Number = m._totalframes;
> >         if (cf<=tf && cf>1) {
> >             m.gotoAndStop(--cf);
> >         } else if (cf==1) {
> >             m.stop();
> >             if(f) eval(f)();
> >             delete m.onEnterFrame;
> >         }
> >     };
> > }
> >
> > But the change was on the way I call the function:
> > _root.rew(d.mask,"_root.nextFrame");
> >
> > So now if I pass it between "" it seems to work fine. But still, there's
> a
> > small problem that bothers me. Now I have to use eval(), wich I don't
> like
> > at all. Any Ideas on how changing eval to a better solution?
> >
> >
> >
> > On 18/02/07, dr.ache <[EMAIL PROTECTED]> wrote:
> > >
> > > Hi.
> > >
> > > i guess you have a scope problem. for me, your code worked
> > > fine, here my adjustments to test it (i got one MC with a 180 frame
> > > MotionTween starting at 120 when loaded, on the stage, called "fred".
> > > i simulated the function call with an interval, so the animation
> starts
> > > and
> > > after 2 seconds, your function "rew" is called with the parameters
> > > "_root.fred" and the function which should be called after rewind the
> > > animation "sayHello".
> > >
> > > It works. When you use Flash 8, try to understand the Delegate Class,
> > > which should help you handle the scope problem.
> > >
> > > hope that helps...
> > > dr.ache
> > >
> > > var i:Number = setInterval(this,"rew",2000,_root.fred,"sayHello");
> > >
> > > function rew(m:MovieClip, f:Function) {
> > >     clearInterval(_root.i);
> > >    m.onEnterFrame = function() {
> > >        var cf:Number = m._currentframe;
> > >        var tf:Number = m._totalframes;
> > >        if (cf<=tf && cf>1) {
> > >            m.gotoAndStop(--cf);
> > >        } else if (cf==1) {
> > >            m.stop();
> > >            if(f) f();
> > >            delete m.onEnterFrame;
> > >        }
> > >    };
> > > }
> > >
> > > function sayHello() {
> > >     trace("hallo");
> > > }
> > >
> > > Felipe Hefler schrieb:
> > > > Hi everyone! I'm having trouble with this issue:
> > > > function rew(m:MovieClip, f:Function) {
> > > >    m.onEnterFrame = function() {
> > > >        var cf:Number = m._currentframe;
> > > >        var tf:Number = m._totalframes;
> > > >        if (cf<=tf && cf>1) {
> > > >            m.gotoAndStop(--cf);
> > > >        } else if (cf==1) {
> > > >            m.stop();
> > > >            if(f) f();
> > > >            delete m.onEnterFrame;
> > > >        }
> > > >    };
> > > > }
> > > >
> > > > When I call this function in an action frame it's like this:
> > > > rew(targetpath_movieClipName, _root.play);
> > > >
> > > > What I want is that the function I've passed as a parameter works
> like
> > a
> > > > function later in the code. Messy explanation?? OK OK...
> > > > Let's see if I can explain better. If I do something like this:
> > > >
> > > > function rew(m:MovieClip, f:String {
> > > >    m.onEnterFrame = function() {
> > > >        var cf:Number = m._currentframe;
> > > >        var tf:Number = m._totalframes;
> > > >        if (cf<=tf && cf>1) {
> > > >            m.gotoAndStop(--cf);
> > > >        } else if (cf==1) {
> > > >            m.stop();
> > > >            if(f == "play") play();
> > > >            delete m.onEnterFrame;
> > > >        }
> > > >    };
> > > > }
> > > >
> > > > it works perfectly, but I want to call any function and don't wanna
> > > > have to
> > > > make a switch for every kind o function.
> > > > Has anyone got it?
> > > >
> > >
> > > _______________________________________________
> > > Flashcoders@chattyfig.figleaf.com
> > > To change your subscription options or search the archive:
> > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> > >
> > > Brought to you by Fig Leaf Software
> > > Premier Authorized Adobe Consulting and Training
> > > http://www.figleaf.com
> > > http://training.figleaf.com
> > >
> > _______________________________________________
> > Flashcoders@chattyfig.figleaf.com
> > To change your subscription options or search the archive:
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > Brought to you by Fig Leaf Software
> > Premier Authorized Adobe Consulting and Training
> > http://www.figleaf.com
> > http://training.figleaf.com
> >
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com


_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to