A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Problems with buttons inside MC added with addChild.

  1. #1
    Junior Member
    Join Date
    May 2004
    Posts
    7

    Problems with buttons inside MC added with addChild.

    I have my Stage. On this I have an empty MovieClip (MC).
    To begin with I do:

    MCContent = new TestMC();
    MC.addChild(MCContent);


    This works.

    But in my TestMC, I have another MovieClip Button01, which is named Button01, where I have the code:

    Button01.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
    function handleMouseUp(event:MouseEvent):void {
    trace('Hello World);
    }


    But when I click on Button01 nothing happens... What am I doing wrong?

  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Code:
    MCContent = new TestMC();
    MC.addChild(MCContent);
    
    
    MCContent.Button01.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
    function handleMouseUp(event:MouseEvent):void {
    trace('Hello World);
    }
    I don't have flash handy to test right now but will help more tonight if need be. But anyway, give this a whirl and let me know what happens.

  3. #3
    Junior Member
    Join Date
    May 2004
    Posts
    7
    That didn't work, got the error:
    "...: Access of undefined property MCContent."

    I tried:
    Object(root).MCContent.ImageButton.addEventListene r(MouseEvent.MOUSE_UP, handleImageMouseUp);
    and that don't report any error, but doesn't do anything

    Any other ideas?

  4. #4
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    So Button01 is in MCContent, right?

    Do you have a var declaration with the MCContent clip?

  5. #5
    Junior Member
    Join Date
    May 2004
    Posts
    7
    Button01 is inside TestMC, which is a MovieClip added to MC via addChild, from the code:
    MCContent = new TestMC();
    MC.addChild(MCContent);

    Does that make sense?

  6. #6
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Code:
    var MCContent:MovieClip = new TestMC();
    MC.addChild(MCContent);
    So you have that, right?

    If I got you now, this should do it:

    Code:
    trace("Here's the line up, if one returns null, we know that it isn't getting set up somewhere");
    trace(MC);
    trace(MC.MCContent);
    trace(MC.MCContent.Button01);
    trace("End of Line up");
    
    MC.MCContent.Button01.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
    function handleMouseUp(event:MouseEvent):void {
         trace("Hello World");
    }
    I added more traces as well to trace out values. They should all return Object MovieClip when traced I beleive.

  7. #7
    Junior Member
    Join Date
    May 2004
    Posts
    7
    I get:
    Access of undefined property MC
    on all 3 lines.

    The code is inside TestMC and NOT my main stage, I guess that's why it cannot find MC.

  8. #8
    Junior Member
    Join Date
    May 2004
    Posts
    7
    If I just have the code:
    trace("Here's the line up, if one returns null, we know that it isn't getting set up somewhere");
    trace(Button01);
    trace("End of Line up");
    Button01.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
    function handleMouseUp(event:MouseEvent):void {
    trace("Hello World");
    }

    I get:
    Here's the line up, if one returns null, we know that it isn't getting set up somewhere
    [object MovieClip]
    End of Line up

    But clikcing on the button doesn't output "Hello World".

  9. #9
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Oh, your button code is in TestMC?

    And you want a click,

    Okay, try this:

    Code:
    Button01.addEventListener(MouseEvent.CLICK, handleMouseClick);
    function handleMouseClick(evt:MouseEvent):void {
         trace("Hello World");
    }
    My apologies, I must have misunderstood you.

    Let me know if this works for you.

  10. #10
    Junior Member
    Join Date
    May 2004
    Posts
    7
    That worked! What is the difference?

  11. #11
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    the MOUSE_UP event is really not meant for the functionality you were after. The CLICK event is what's meant to handle the event of capturing a mouse press. The MOUSE_UP event is kinda sketchy honestly. It's made to handle when the mouse is let up but it doesn't always do 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