A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: "Clicking" a button without clicking it?

  1. #1

    How to "click" a button without clicking it?

    I have a standard button, and I want to send a "virtual" mouse click to it using actionscript (i.e., in-addition to clicking it with the mouse). I CAN'T use a function to define the code within the button.

    buttonName.onRelease = function () {
    //code
    }

    How can I use actionscript to send a "virtual" mouse click to that button?

    Thank you!!!
    Last edited by smcneilly; 12-11-2004 at 12:21 AM.

  2. #2
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    So, what would happen then? Whenever a person clicks the mouse button the action would be called? Do you want the button to look as though it is being pressed, even when it isn't? What's keeping you from using one giant invisible button?

  3. #3
    Learn more, know less.
    Join Date
    Jul 2004
    Posts
    169

    Here's a thought...

    What if the button was actually a movie clip instead?

    If you want the button be clickable by the real mouse, then make it an actual button and put the regular functions in it like you demonstrated. Then put it inside a movie clip. Have the click trigger the animation of the virtual mouse.

    If you want to click somewhere else and then have the virtual mouse go click a button, make a huge hit state (giant invisible button) and make the function of the button go to another point in your movie clip where the virtual mouse is merely animated to look like it is clicking a button.

    If you just want a virtual mouse to click a button without any interaction from the user at all, then just animate it all and put whatever actions you want in the last frame of the mc.

    I hope I explained that well enough, and I hope it helps.

    --J

  4. #4
    Quixx and JoelTHE,

    Thank you for your responses... but, I cannot implement the suggestions you provided, due to the structure of my project. This is not an animation project. It's a software application.

    To explain my project would be far too lengthy. My project is EXTREMELY complex, whereby buttons are generated dynamically based on ever-changing database info. So, my buttons are created at runtime.

    To simplify my question, I have a need to:
    1) Execute the code within a button by clicking the button,
    AND
    2) Execute the code within a button WITHOUT clicking the button, but RATHER by using a "command" in actionscript.

    In other words, here's my button:
    buttonName.onRelease = function() {
    //dynamically-generated code that will be executed...
    }

    My question is, is there a way to build code that says:
    "Execute the code within the button called buttonName"?

  5. #5
    Learn more, know less.
    Join Date
    Jul 2004
    Posts
    169

    Global Functions.

    What if you make a global function that you can call with or without the button, or from any MC for that matter...? Just a standalone function that contains your dynamic variables outside of the button, then your button would call that function instead of having the dynamic code generated within the button code itself...?

    OR, what if your button created a global function? I don't know if that's possible, but that would allow you to call the function from outside the button, wouldn't it?

    Keep posting if this doesn't help, more info about the project might be needed.

    --J


    P.S. -- Sorry about the last post, I misunderstood your goal.
    Last edited by JoelTHE; 12-11-2004 at 08:21 PM.

  6. #6
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Generate the code that will be executed in some_function, and assign this function to your button handler...

    buttonName.onRelease = function() {
    //dynamically-generated code that will be executed...
    some_function();
    }

    function some_function(){
    trace("YO!");
    };

    The button when clicked should output the trace, but you can also call the same function from anywhere, with...

    some_function();

  7. #7
    Learn more, know less.
    Join Date
    Jul 2004
    Posts
    169

    Yeah, what he said.

    That was a much better of explaining what I meant.

  8. #8
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    My question is, why have a button at all when you can click anywhere on the screen and have the effect happen?

    From what I can make of your needs, I wouldn't use an actual button at all. I'd use a movie clip which I could dynamically call a button function on (if need be). So if you want the movie clip to act like it is hit when the mouse is clicked, you'd need a movie clip with at least two frames. Frame one would have the button in it's normal state, while frame two would have the button in it's hit state. If you'd like the cursor to change into the hand pointer, just put an invisible button within the movie clip. Then put this in the timeline (where "button" is the instance name of the button movie clip):

    code:

    button.onMouseDown = function() {
    this.gotoAndPlay("button_hit");
    trace("other code you'd like to perform");
    };



    So lets say you want 10 "button" movies dynamically attached to your main movie on load, with their x and y positions set randomly. Also, you'd like them to all drop off the screen when the mouse button is pressed. You could do this:
    code:

    for (i=0; i<10; i++) {
    attachMovie("button", "button"+i, i, {_x:i*50, _y:100});
    this.drop = false;
    _root["button"+i].onMouseDown = function() {
    this.gotoAndPlay("button_hit");
    this.drop = true;
    };
    _root["button"+i].onEnterFrame = function() {
    if (this.drop) {
    this._y += 10;
    }
    };
    }


    NOTE: I've attached a zip file of this working.

    Is this close to what you need to happen?

  9. #9
    Thanks again for your responses!

    Quixx: I completely understand what you're saying, and I appreciate your ideas; however it doesn't apply to my application. I need my user to click a specific button when they need to (which is on a specific part of the screen), but I also need my application to execute the code within the button, under certain circumstances. In other words, sometimes the user must execute the code within the button... other times, the application must execute the code within the button.

    oldnewbie and JoelTHE: What you suggested is exactly what I feared! Using a function in this manner was going to be my last resort, but I'll have to do it. I'm going to have to re-write a ton of code to accomodate this method. But, I'll use the method you suggested. So, if this is what experts such as yourself recommend, then that's what I'll do! BTW, JoelTHE, I tried getting my button to create a global function, but Flash kept telling me that it was illegal. Maybe I was doing something wrong...

    Thank you all again.

  10. #10
    Learn more, know less.
    Join Date
    Jul 2004
    Posts
    169

    Expert? Ha!

    Oldnewbie, yes, he's an expert. Half the time I'm talking out of my @ss.

    Anyway, I'm glad our method will work for you, even if it means a lot of extra work. Sometimes the real greatness is in the details.

    I wasn't even sure if a button could create a global function, I was just kind of thinking out loud, so no worries if it's not possible. The other way will be more controllable and easier to manage anyway.

    --J

  11. #11
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    Originally posted by smcneilly
    Quixx: I completely understand what you're saying, and I appreciate your ideas; however it doesn't apply to my application. I need my user to click a specific button when they need to (which is on a specific part of the screen), but I also need my application to execute the code within the button, under certain circumstances. In other words, sometimes the user must execute the code within the button... other times, the application must execute the code within the button.
    Oh... I misunderstood what you were looking for. After reading, "How can I use actionscript to send a "virtual" mouse click to that button?", I thought you were looking to have the action happen everytime a user clicked the mouse. Sorry about that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center