Skip to main content
Known Participant
January 7, 2011
Answered

How to trigger a function

  • January 7, 2011
  • 3 replies
  • 608 views

In stage Number of MovieClips is there.

and MovieClips inside some functions is there .

iam giveing dynamic property to MovieClips same as  function name.

because iam click the MovieClip call the function.

mc.functionName="dosome()"

mc.addEventListener(MouseEvent.CLICK,call_inside_function)

function callinsidefunction(event:MouseEvent){

/// event.target.functionName

}

/// mc  inside function

function dosome(){

trace("hi")

}

// end of mc  inside function

This topic has been closed for replies.
Correct answer Rui Silva

Hi,

You can try to reference the function using square brackets. Something like this:

mc.functionName="dosome()"

mc.addEventListener(MouseEvent.CLICK,callinsidefunction)

function callinsidefunction(event:MouseEvent){

     event.target[event.target.functionName]();

}

/// mc  inside function

function dosome(){

trace("hi")

}

// end of mc  inside function

I've just noticed that you spelled the event handler name incorrectly in the addEventListener method, so I've corrected it.

Cheers,

Rui

3 replies

January 7, 2011

mc.functionName="dosome"

mc.addEventListener(MouseEvent.CLICK,call_inside_function)

function call_inside_function(event:MouseEvent){
var str:String=event.target.functionName;
this[str]();
}

function dosome(){
trace("hi")
}

Ned Murphy
Legend
January 7, 2011

Use:

mc.functionName="dosome"; // do not use () here

mc.addEventListener(MouseEvent.CLICK,call_inside_function)

function call_inside_function(event:MouseEvent){  // name the same as in event listener

event.target[event.target.functionName]();

}

Rui Silva
Rui SilvaCorrect answer
Inspiring
January 7, 2011

Hi,

You can try to reference the function using square brackets. Something like this:

mc.functionName="dosome()"

mc.addEventListener(MouseEvent.CLICK,callinsidefunction)

function callinsidefunction(event:MouseEvent){

     event.target[event.target.functionName]();

}

/// mc  inside function

function dosome(){

trace("hi")

}

// end of mc  inside function

I've just noticed that you spelled the event handler name incorrectly in the addEventListener method, so I've corrected it.

Cheers,

Rui