Skip to main content
MrKnives
Known Participant
May 21, 2011
Answered

Copying functions in ActionScript 3 (to duplicate a function then use it)

  • May 21, 2011
  • 2 replies
  • 893 views

I'm curious to know if there is any examples you all could give me any examples of copying a function in AS3; then use that copy independent of the original function it copied.

I want to make an animation system that heavily depends on one type of function. Since it depends on one type of function, I cant have every single piece of code calling back to the same function if I want to use it multiple times.

Is there a way of properly doing this? are their performance consequences?

I'd really like to do this instead of writing several different functions out separately that work nearly the same. (plus I need to feed a set of values into those functions that are created, some being called by event listeners of course)

Anybody have any ideas or can point me in the right direction?

This topic has been closed for replies.
Correct answer blazejewicz

Hi,

@MrKnives

I think you could create instance of object that could encapsulate your set of that data and then execute it to process data. It could be as simple as some util class that once created expose method you could call at any later time to process given set of data.

If you want to decouple such implementation from your actual context i think you could apply Action/Command patter, see:

http://en.wikipedia.org/wiki/Command_pattern

object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

http://www.google.com/search?btnG=1&pws=0&q=Action+command+pattern+ActionScript

regards,

Peter

2 replies

blazejewiczCorrect answer
Participating Frequently
May 22, 2011

Hi,

@MrKnives

I think you could create instance of object that could encapsulate your set of that data and then execute it to process data. It could be as simple as some util class that once created expose method you could call at any later time to process given set of data.

If you want to decouple such implementation from your actual context i think you could apply Action/Command patter, see:

http://en.wikipedia.org/wiki/Command_pattern

object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

http://www.google.com/search?btnG=1&pws=0&q=Action+command+pattern+ActionScript

regards,

Peter

Ned Murphy
Legend
May 21, 2011

What makes you conclude that you cannot have the same function called multiple times?

Inspiring
May 21, 2011

correct me if I'm wrong but I think he is trying to pass a Function object to multiple other objects, say .swf files.  I just ran into this problem.  The thing is, is that if you pass a Function object to one swf file, then you can't pass that same function to another swf.  Or atleast that's what seemed to happen to me.  I worked my way around it by passing the main .swf to the loaded .swfs and then using that reference to call the function.