Link to home
Start Free TrialLog in
Avatar of luigidenaro
luigidenaro

asked on

Actionscript Simulate button click

Dear Experts,

I have a flash button object and I need to execute a click event on it through an external function.

Eg:
function execute_but_click()
{
 obj_button.onRelease(); //or something like that.. is it possible?
}

Thx in Advance,
Luigi.
Avatar of CyanBlue
CyanBlue
Flag of United States of America image

Howdy...  :)

What about this sort of approach???

// Here you are defining a function that will be executed when a button is pressed...
function actionForButton()
{
      trace("Hello...  A button just got attention...");
}

// Here you are assigning that function to the given button object...
obj_button.onRelease = actionForButton;

...
...
...

// This function will execute the button click when you execute this execute_but_click()
// function...  If you do not have multiple commands to execute, you do not need this
// function...  You only need to call actionForButton() function in that case...
function execute_but_click()
{
      ...
      ...
      actionForButton();
      ...
      ...
}

G'day...  :)

Hi Luigi,

The answer depends on whether you are to use the function on other buttons.

If its not then simply do like this:

// Assign the function directly to the button
obj_button.onRelease = function(){
  trace("Hello")
}



A more reusable approach:

// Create a prototype using the Button class
Button.prototype.myButtonFunction = function(parameters){
  // This is the function  
  this.onRelease  = function(){
    trace(parameters)
  };
};
// Assign the prototype and hereby the function nested in the prototype to your buttons
obj_button1.myButtonFunction("Hello")
obj_button2.myButtonFunction("World")



Why use the prototype?
Simply to gain control over both function and eventhandlers.

Let's say you want to change the event onRelease to onRollOver.
If you did like this:

function myButtonFunction (parameters){
  trace(parameters)
}
obj_button1.onRelease = function(){ myButtonFunction ("Hello")  }
obj_button2.onRelease = function(){ myButtonFunction ("World") }

Then you need to change the onRelease on all your buttons (what if you had 50)
Using the prototype you only need to change it one place.


Best, Jakob E







Avatar of luigidenaro
luigidenaro

ASKER

Hi guys,

I think I was not clear enough..

Im coping and commenting my code to explain me better...

This code is from a chat login frame where I detect if the user pressed the ENTER key and then I TRY to execute a button conect (but_connect) I have in the page.

What I need is.. when the user press the ENTER key, I need simulate the same as the user move the mouse over the button connect (but_connect) and Press it.. Just it.... Execute the onRelease function from the button like it was manually pressed.

keypress_listener_f1 = new Object();
keypress_listener_f1.onKeyUp = function ()
{
            code.text = Key.getCode();
            if (Key.getCode() == 13)
            {
                                        // ************************************************
                    // HERE IS WHERE I NEED TO EXECUTE THE BUTTON CLICK.      
                                     
                                       but_connect.NEEDTOCLICKIT();

                                        // ************************************************
            }
}

Key.addListener(keypress_listener_f1);
      
stop();

Thx again!

Regards,
Luigi.
ASKER CERTIFIED SOLUTION
Avatar of Jakob_E
Jakob_E
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Jacob,

This is not exactly the idea I had in mind about button click simulation, but It feets perfect to solve my problem.

I just did a movieclip with 2 frames (mv_do_login) and in frame 2 I have a call to the function I need..
Then I solved the problem with just one line when the user press ENTER KEY : _root.mv_do_login.gotoAndPlay(2);

The solution is not so elegant as urs, but it do what I need.

Thx u very much for ur help.

Regards,
Luigi.
You are welcome :)