dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: help with listener?

  1. #1
    Senior Member
    Join Date
    Dec 2004
    Posts
    101

    help with listener?

    can anyone break down this code for me, i mean can someone please refrase the code to where i can make sense of it, and how i would use it? "laymans terms"

    thank you as always:

    Code:
    var listenerObject.eventName = function(){
       // your code here
    };
    Code:
    broadcasterObject.addListener(listenerObject);
    Code:
    broadcasterObject.removeListener(listenerObject);

  2. #2
    Junior Member
    Join Date
    Dec 2004
    Location
    UK, Gloustershire
    Posts
    8
    hi

    looks like:

    var listenerObject.eventName = function(){

    is starting a function, this is where you can put some code if it has to be called in a loop or if you want to call it multiple times to have wrighting it out (also make the program run quicker). listenerobject is the name of the function that is called therefore:

    broadcasterObject.addListener(listenerObject);

    will start the function, broadcasterobject being the thing that calles it and:

    broadcasterObject.removeListener(listenerObject);

    ends the function

    hope that helps, looks right

  3. #3
    Member
    Join Date
    May 2004
    Posts
    33
    hello,

    i happen to have a small prob with listeners as well


    code:

    var listener: Object = new Object();
    listener.click = function()
    {square._x = square._x +10 };
    right_btn.addEventListener("click",listener);




    ive named my button right_btn and my mc square
    when i click on the button it should move the square
    10px to the right but for some reason it doesnt work

  4. #4
    Junior Member
    Join Date
    Dec 2004
    Location
    UK, Gloustershire
    Posts
    8
    sory know idea :S, never realy got into doing functions in flash, im shure somone will know though

    sory

  5. #5
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877

    ??

    Are you trying to use the right arrow key on the keyboard to do this, or a button MC you have made?

  6. #6
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Originally posted by amaterasu
    hello,

    i happen to have a small prob with listeners as well


    code:

    var listener: Object = new Object();
    listener.click = function()
    {square._x = square._x +10 };
    right_btn.addEventListener("click",listener);




    ive named my button right_btn and my mc square
    when i click on the button it should move the square
    10px to the right but for some reason it doesnt work
    That code would assume that right_btn is an instance of the button component, not a regular button symbol.

    using a normal button symbol you could use something like this instead of a listener,

    code:

    right_btn.onPress = function() {
    square._x += 10; // a shorter way of writing square._x = square._x +10
    };


  7. #7
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222

    Re: help with listener?

    Originally posted by iliad2b
    can anyone break down this code for me, i mean can someone please refrase the code to where i can make sense of it, and how i would use it? "laymans terms"

    thank you as always:

    Code:
    var listenerObject.eventName = function(){
       // your code here
    };
    Code:
    broadcasterObject.addListener(listenerObject);
    Code:
    broadcasterObject.removeListener(listenerObject);
    In flash many objects have events associated with them, for example the mouse object has events like onMouseDown, onMouseMove etc, the Stage object has onResize and so on.

    with listeners you can create objects that listen to the events broadcast by other objects so they can respond to them.

    for example,

    code:

    myListenerObject = {}; // create a new object, this object will be used to listen for mouse events
    // create a method to be called when the mouse broadcasts an onMouseDown event
    myListenerObject.onMouseDown = function() {
    trace("the mouse was pressed");
    };
    // and one to be called when the mouse broadcasts an onMouseUp event
    myListenerObject.onMouseUp = function() {
    trace("the mouse was released");
    };
    // now make the object listen to the mouse object
    Mouse.addListener(myListenerObject);



    some links...

    another listener example - this one uses the stage object to make items in a movie resize and reposition themseleves when the movie gets resized
    senocular's listener tutorial

  8. #8
    Member
    Join Date
    May 2004
    Posts
    33
    Originally posted by catbert303
    That code would assume that right_btn is an instance of the button component, not a regular button symbol.

    using a normal button symbol you could use something like this instead of a listener,

    code:

    right_btn.onPress = function() {
    square._x += 10; // a shorter way of writing square._x = square._x +10
    };

    ah i see so i cannot use a simple button symbol when coding it like that it has to be a button component ?!

  9. #9
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    yep

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