The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 11 of 11

Thread: Add eventlistener to embedded movieclip AS3

  1. #1

    Add eventlistener to embedded movieclip AS3

    How can I addeventlistener to a movieclip embedded in another to where both the parent and the child will respond to a mouse event in AS3? The parent movieclip will move while the child changes alpha.

  2. #2
    12
    posts
    Registered User
    Try

    Code:
    ParentMovieClip.mouseChildren = false;
    That way, events which are targeting child objects will get fired, but the target will be the parent.

  3. #3
    Quote Originally Posted by bLaf View Post
    Try

    Code:
    ParentMovieClip.mouseChildren = false;
    That way, events which are targeting child objects will get fired, but the target will be the parent.

    I'll try it. Thanks

  4. #4
    If your movieclip is a child of another movieclip then you can register the parent movieclip for listener and in that listener function you can call event.currentTarget this will give the the child movieclip which is inside the other so by registering one listener you can do both things you want moving the parent and changing alpha for the child.

  5. #5
    Quote Originally Posted by phillyrob View Post
    How can I addeventlistener to a movieclip embedded in another to where both the parent and the child will respond to a mouse event in AS3? The parent movieclip will move while the child changes alpha.
    Here's the basic concept:
    "parent1" clip is the target for the event listener.
    Event propagation causes the children of "parent1" to receive events directed at "parent1".
    "parent1" is tweened within the function triggered by the child clips, "child1" and "child2".
    Code:
    //on the stage: movieclip "parent1" contains movieclips "child1" and "child2"
    stop();
    import gs.TweenMax;
    import fl.motion.easing.*;
    var button:String = "";
    
    parent1.addEventListener(MouseEvent.MOUSE_OVER, rollover, false, 0, true);
    parent1.addEventListener(MouseEvent.MOUSE_OUT, rollout, false, 0, true);
    
    function rollover(event:MouseEvent):void {
    	button = event.target.name;
    	TweenMax.to(event.target, 1, {alpha:.5});
    	TweenMax.to(parent1, .2, {x:120, ease:Linear.easeIn});
    	trace(button);
    }
    
    function rollout(event:MouseEvent):void {
    	button = event.target.name;
    	TweenMax.to(event.target, 1, {alpha:1});
    	TweenMax.to(parent1, 1, {x:100, ease:Elastic.easeOut});
    	trace(button);
    }

  6. #6
    Quote Originally Posted by snickelfritz View Post
    Here's the basic concept:
    "parent1" clip is the target for the event listener.
    Event propagation causes the children of "parent1" to receive events directed at "parent1".
    "parent1" is tweened within the function triggered by the child clips, "child1" and "child2".

    ActionScript Code:
    //on the stage: movieclip "parent1" contains movieclips "child1" and "child2"</p>
    <p>stop();</p>
    <p>import gs.TweenMax;</p>
    <p>import fl.motion.easing.*;</p>
    <p>var button:String = "";</p>
    <p>&nbsp;</p>
    <p>parent1.addEventListener(MouseEvent.MOUSE_OVER, rollover, false, 0, true);</p>
    <p>parent1.addEventListener(MouseEvent.MOUSE_OUT, rollout, false, 0, true);</p>
    <p>&nbsp;</p>
    <p>function rollover(event:MouseEvent):void {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;button = event.target.name;</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;TweenMax.to(event.target, 1, {alpha:.5});</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;TweenMax.to(parent1, .2, {x:120, ease:Linear.easeIn});</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;trace(button);</p>
    <p>}</p>
    <p>&nbsp;</p>
    <p>function rollout(event:MouseEvent):void {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;button = event.target.name;</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;TweenMax.to(event.target, 1, {alpha:1});</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;TweenMax.to(parent1, 1, {x:100, ease:Elastic.easeOut});</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;trace(button);</p>
    <p>}
    Thanks Snickelfritz, I will try to digest and implement you code.

  7. #7
    Quote Originally Posted by phillyrob View Post
    I'll try it. Thanks
    Thanks, but I am not following. Can you give me an example? Here is some simple code that is on the root or parent timeline. I have a movieclip called recTangle, Inside the movieclip recTangle, I have a movieclip called bluerecTangle. I want the movieclip recTangle to move and at the sametime I want the embedded movieclip bluerecTangle to change alpha to = 0;


    recTangle.addEventListener(MouseEvent.ROLL_OVER, rollOver1);
    recTangle.addEventListener(MouseEvent.ROLL_OUT, rollOut1);
    function rollOver1(event:MouseEvent):void
    {
    recTangle.x += 50;

    }
    function rollOut1(event:MouseEvent):void
    {
    recTangle.x -= 50;
    }
    Last edited by phillyrob; May 12th, 2008 at 02:47 AM. Reason: forgot code

  8. #8
    Quote Originally Posted by adnan794 View Post
    If your movieclip is a child of another movieclip then you can register the parent movieclip for listener and in that listener function you can call event.currentTarget this will give the the child movieclip which is inside the other so by registering one listener you can do both things you want moving the parent and changing alpha for the child.
    Thanks, but I am not following this. Can you please give me an example? Here is some simple code that is on the root or parent timeline. I have a movieclip called recTangle, Inside the movieclip recTangle, I have a movieclip called bluerecTangle. I want the movieclip recTangle to move and at the sametime I want the embedded movieclip bluerecTangle to change alpha to = 0;


    recTangle.addEventListener(MouseEvent.ROLL_OVER, rollOver1);
    recTangle.addEventListener(MouseEvent.ROLL_OUT, rollOut1);
    function rollOver1(event:MouseEvent):void
    {
    recTangle.x += 50;

    }
    function rollOut1(event:MouseEvent):void
    {
    recTangle.x -= 50;
    }
    Last edited by phillyrob; May 12th, 2008 at 02:46 AM. Reason: forgot to add code

  9. #9
    Quote Originally Posted by snickelfritz View Post
    Here's the basic concept:
    "parent1" clip is the target for the event listener.
    Event propagation causes the children of "parent1" to receive events directed at "parent1".
    "parent1" is tweened within the function triggered by the child clips, "child1" and "child2".

    ActionScript Code:
    //on the stage: movieclip "parent1" contains movieclips "child1" and "child2"</p>
    <p>stop();</p>
    <p>import gs.TweenMax;</p>
    <p>import fl.motion.easing.*;</p>
    <p>var button:String = "";</p>
    <p>&nbsp;</p>
    <p>parent1.addEventListener(MouseEvent.MOUSE_OVER, rollover, false, 0, true);</p>
    <p>parent1.addEventListener(MouseEvent.MOUSE_OUT, rollout, false, 0, true);</p>
    <p>&nbsp;</p>
    <p>function rollover(event:MouseEvent):void {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;button = event.target.name;</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;TweenMax.to(event.target, 1, {alpha:.5});</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;TweenMax.to(parent1, .2, {x:120, ease:Linear.easeIn});</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;trace(button);</p>
    <p>}</p>
    <p>&nbsp;</p>
    <p>function rollout(event:MouseEvent):void {</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;button = event.target.name;</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;TweenMax.to(event.target, 1, {alpha:1});</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;TweenMax.to(parent1, 1, {x:100, ease:Elastic.easeOut});</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;trace(button);</p>
    <p>}

    How could I do it with is code:

    Here is some simple code that is on the root or parent timeline. I have a movieclip called recTangle, Inside the movieclip recTangle, I have a movieclip called bluerecTangle. I want the movieclip recTangle to move and at the sametime I want the embedded movieclip bluerecTangle to change alpha to = 0;


    recTangle.addEventListener(MouseEvent.ROLL_OVER, rollOver1);
    recTangle.addEventListener(MouseEvent.ROLL_OUT, rollOut1);
    function rollOver1(event:MouseEvent):void
    {
    recTangle.x += 50;

    }
    function rollOut1(event:MouseEvent):void
    {
    recTangle.x -= 50;
    }

  10. #10
    12
    posts
    Registered User
    Quote Originally Posted by phillyrob View Post
    Thanks, but I am not following. Can you give me an example? Here is some simple code that is on the root or parent timeline. I have a movieclip called recTangle, Inside the movieclip recTangle, I have a movieclip called bluerecTangle. I want the movieclip recTangle to move and at the sametime I want the embedded movieclip bluerecTangle to change alpha to = 0;


    recTangle.addEventListener(MouseEvent.ROLL_OVER, rollOver1);
    recTangle.addEventListener(MouseEvent.ROLL_OUT, rollOut1);
    function rollOver1(event:MouseEvent):void
    {
    recTangle.x += 50;

    }
    function rollOut1(event:MouseEvent):void
    {
    recTangle.x -= 50;
    }


    Here's my version

    Code:
     recTangle.addEventListener(MouseEvent.ROLL_OVER, rollOver1);
    recTangle.addEventListener(MouseEvent.ROLL_OUT, rollOut1);
    recTangle.mouseChildren = false;
    
    function rollOver1(event:MouseEvent):void
    {
    recTangle.x += 50;
    // two ways to deal with it:
    recTangle.bluerecTangle._alpha = 0;
    // or
    //event.currentTarget.bluerecTangle._alpha = 0;
    
    }
    function rollOut1(event:MouseEvent):void
    {
    recTangle.x -= 50;
    
    recTangle.bluerecTangle._alpha =100;
    // or
    // event.currentTarget.bluerecTangle._alpha = 100;
    
    
    }

  11. #11

    Quote Originally Posted by bLaf View Post
    Here's my version

    Code:
     recTangle.addEventListener(MouseEvent.ROLL_OVER, rollOver1);
    recTangle.addEventListener(MouseEvent.ROLL_OUT, rollOut1);
    recTangle.mouseChildren = false;
     
    function rollOver1(event:MouseEvent):void
    {
    recTangle.x += 50;
    // two ways to deal with it:
    recTangle.bluerecTangle._alpha = 0;
    // or
    //event.currentTarget.bluerecTangle._alpha = 0;
     
    }
    function rollOut1(event:MouseEvent):void
    {
    recTangle.x -= 50;
     
    recTangle.bluerecTangle._alpha =100;
    // or
    // event.currentTarget.bluerecTangle._alpha = 100;
     
     
    }
    Thanks 1 million this works pefectly!. I changed the code _alpha to alpha to work in AS 3 but it works. You are a life saver. Thanks again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012