dcsimg
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Help with attach script to button component

  1. #1
    Grandmaster Flash
    Join Date
    Apr 2004
    Location
    Edinburgh, Scotland
    Posts
    139

    Help with attach script to button component

    Hi,

    Can anyone help me out by telling me what the code for something like:

    myButton.onMouseDown = function() { _parent.doMouseDown(this); }

    becomes when I want to attach the action to a pushbutton component?

    I have trued such things as:

    myButton.on(click) = function() { _parent.doMouseDown(this); }

    but doesn't work.

    Thanks in advance.
    If it ain't broke, don't fix it.

  2. #2
    Senior Member sprout74's Avatar
    Join Date
    Oct 2000
    Posts
    134
    dont really understand the post.

    are you trying to subscribe event listeners to the button?

    pass the reference to the button that is clicked to another method in its parent?

    ??

    sprout
    "If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea."
    from The Zen of Python (by Tim Peters)

  3. #3
    Grandmaster Flash
    Join Date
    Apr 2004
    Location
    Edinburgh, Scotland
    Posts
    139
    Hi Sprout,

    What I am doing is loading in a .swf file. In this .swf file I have a pushbutton component. When I load the .swf into the parent movie, I want to assign a click event to the button, which will fire an event which will be caught in the parent movie.

    Hope this makes sense.

    Thanks

    GW
    If it ain't broke, don't fix it.

  4. #4
    Senior Member sprout74's Avatar
    Join Date
    Oct 2000
    Posts
    134
    once the movie has loaded into its parent, have the parent movie assign an event listener to the button. Then the parent movie can be the subscriber to the mouse events.

    check out the Help files that ship with Flash MX.

    There are nice examples of how to use event listeners there.

    Then you can have the parent receive the events from the button in the loaded SWF.

    ok?
    "If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea."
    from The Zen of Python (by Tim Peters)

  5. #5
    Grandmaster Flash
    Join Date
    Apr 2004
    Location
    Edinburgh, Scotland
    Posts
    139
    Thanks for your help Sprout - still only getting started with Actionscript since version 4!
    If it ain't broke, don't fix it.

  6. #6
    Senior Member sprout74's Avatar
    Join Date
    Oct 2000
    Posts
    134
    here is a tutorial i found that is an introduction to event objects.

    and another one

    the idea is that the thing that triggers the event, like a mouse button, tells other MovieClips/objects that the event has occured and then they can react to it if they want to.

    it is an excellent way to handle events in Flash.

    So, once you know the path to the button that is loaded, once it is loaded, the main movie adds a listener to the button in the loaded clip, so that when it is clicked, the main movie is told about the event.

    have a go,

    sprout
    "If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea."
    from The Zen of Python (by Tim Peters)

  7. #7
    Grandmaster Flash
    Join Date
    Apr 2004
    Location
    Edinburgh, Scotland
    Posts
    139
    Sprout, thanks again for links to the tutorials.

    I have completed a couple of the tutorials and tried an adaptation for the button component.

    I have added a pushbutton component on one layer of my flash movie. I have also added the following script:

    [actionscript]//butt1.text = "initial content";
    myListener = new Object();
    myListener.onClick = function() {
    trace("clicked button");
    };
    butt.addListener(myListener);
    stop();[/actionscript]

    My button instance name is 'butt'. However when I compile the movie and run it nothing happens.

    Any ideas?

    Thanks in advance.
    If it ain't broke, don't fix it.

  8. #8
    Senior Member sprout74's Avatar
    Join Date
    Oct 2000
    Posts
    134
    i think you have to pass an event object as a parameter of your
    function.


    Code:
    myListener = new Object();
    myListener.onClick = function(oObject) {
    trace("clicked button");
    
    for (var v in oObject) {
      trace (" v: " + v + "  oObject[v]: " + oObject[v]);
    }
    
    };
    butt.addListener(myListener);
    stop();
    The 'for' loop is just to show what kind of properties are passed in in the oObject parameter.
    "If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea."
    from The Zen of Python (by Tim Peters)

  9. #9
    Grandmaster Flash
    Join Date
    Apr 2004
    Location
    Edinburgh, Scotland
    Posts
    139
    Sprout,

    Thanks for the quick reply, but it still wont work.

    GW
    If it ain't broke, don't fix it.

  10. #10
    Grandmaster Flash
    Join Date
    Apr 2004
    Location
    Edinburgh, Scotland
    Posts
    139
    Sprout - managed a simpler solution:

    code:
    butt.onPress=function(){
    trace("Our button was pressed");
    }



    Doesn't use listener, but works! I think I'll get a good book on AS 2 / listeners.

    Thanks.
    If it ain't broke, don't fix it.

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