?
Solved

Actionscript Simulate button click

Posted on 2005-04-09
6
Medium Priority
?
2,563 Views
Last Modified: 2009-12-16
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.
0
Comment
Question by:luigidenaro
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:CyanBlue
ID: 13746153
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...  :)
0
 
LVL 9

Expert Comment

by:Jakob_E
ID: 13746893

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







0
 

Author Comment

by:luigidenaro
ID: 13748711
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
Jakob_E earned 750 total points
ID: 13749297


Well I haven't yet found an elegant way of solving your problem, but what about a workaround!?

Instead of using a real button I'll use a movieclip and then assign the button properties to make
it look and (almost) act like a button.

First without adding any script, create a movieclip with 3 frames:
Frame 1 : Up-state
Frame 2 : Over-state
Frame 3 : Down-state
... you wont need the Hit-State

If you already have a button go to the library right click it and change the properties
to movie clip... Leaving you with a 4 frame movieclip.


Now create a new layer naming it "actions" or whatever. In the first frame add this:

// Prototype making the movieclip simulate to be a button
// What it does is basically to swap the states during onRollOver, onRelease etc.
// I've added a parameter "callFunction" which fires a function you specify.
// In this case the callFunction is added to the event onRelease () but feel
// free to place it where you want :)
MovieClip.prototype.movieButton = function(callFunction) {
  this.gotoAndStop(1);
  this.onRollOver = function() { this.gotoAndStop(2); };
  this.onRollOut = function() { this.gotoAndStop(1); };
  this.onPress = function() { this.gotoAndStop(3); };
  this.onRelease = function() { this.gotoAndStop(2); eval(callFunction());};
  this.onReleaseOutside = function() { this.gotoAndStop(1); };
  this.onDragOver = function() { this.gotoAndStop(1); };
  this.onDragOut = function() { this.gotoAndStop(2); };
};

// Add the "button" to the stage (you can also add it manually)... your choice
myWannabeeButton = attachMovie("movieClipButton", "myWannabeeButton", 0);

// Assign the prototype functionality using ".movieButton()"
// If you want to add functionality like shown below add the function name you want executed as parameter
myWannabeeButton.movieButton(releaseMe);

// The function called on relese
function releaseMe(){ trace("Thanks!"); }


// **********************************************
// Your part  
// **********************************************
keypress_listener_f1 = new Object();

// Enter - Down
keypress_listener_f1.onKeyDown = function() {
  if (Key.getCode() == 13) {
    // When enter is pressed, this will fire the onPress event of your "button"
    myWannabeeButton.onPress();      
  }
};

// Enter - Up
keypress_listener_f1.onKeyUp = function() {
  if (Key.getCode() == 13) {
    // When enter is pressed, this will fire the onRelease event of your "button"
    // This will also execute function releaseMe()
    myWannabeeButton.onRelease();
    // Since there are more state using the mouse I make the "button" turn
    // back to it's default state using the onRollOut
    myWannabeeButton.onRollOut();
  }
};

Key.addListener(keypress_listener_f1);


I know this is a workaround and I hope to find a "real" solution...
If I do I'll drop you a post here :)

~ Jakob E

0
 

Author Comment

by:luigidenaro
ID: 13750265
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.
0
 
LVL 9

Expert Comment

by:Jakob_E
ID: 13751325
You are welcome :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question