A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: How to use a listen function from a movieclip

  1. #1
    Senior Member
    Join Date
    Oct 2005
    Posts
    157

    Arrow How to use a listen function from a movieclip

    Hello,

    I am trying to put a command in one of my movieclips (mc1) that will say:

    When mc2 is at frame 20 (or has done a function assigned previously),
    gotoandplay frame 5 (in mc1).


    Please keep in mind I am going to be putting this code inside the mc1.

    I am trying to design my button (mc1) to dim when mc2 or mc3 is clicked (and mc2 to dim when mc1 or mc3 is clicked, etc.)

    I have the dim tween on frame 5, and the clicked frame on frame 20, in my example.



    Please let me know if this is confusing,
    Thanks in advance

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Why do you think this code should be in mc1? It should be somewhere that knows about both mc1 and mc2.

  3. #3
    Senior Member
    Join Date
    Oct 2005
    Posts
    157
    Its a little confusing to explain, but i know for a fact i cannot do it like that using event listeners and such. I don't know much coding so if you could help me out, here is my file:

    https://rapidshare.com/files/2176109380/website.zip

    I am trying to make it so that when the buttons go down, when you click on one, it goes a little darker, then when you click on another button, it dims back lighter, and the one you click on becomes darker.

    I also have a separate issue where in my contact page, you can't click into the input text fields/boxes (yes they're embedded). If you could help me fix this problem as well I would greatly appreciate it!

    Thanks in advance,
    SK

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you don't know coding, how can you be sure that you can't do it with event listeners? And if you're sure you can't do it with event listeners, why did you title this thread "How to use a listen function from a movieclip"?

    Here's an outline for how to do it. This is psuedocode and I have left some stuff undefined.
    Code:
    var buttons:Array = [mc1, mc2, mc3];
    var current:MovieClip = null;
    
    for (var i:int = 0; i < buttons.length; i++){
      buttons[i].addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
    }
    
    function buttonPress(e:MouseEvent):void{
      if (current != null){
         //code to lighten current.
         current.alpha = 0.3; //replace this with whatever you want to "undo" the press effect
      }
      current = MovieClip(e.currentTarget);
      current.alpha = 1; //again, replace with whatever effect you want on press.
    }
    I have nothing to open fla files with so I cannot help you with your other problem unless you actually explain it.

  5. #5
    Senior Member
    Join Date
    Oct 2005
    Posts
    157
    Sorry for the misunderstanding,
    but thank you so much! I just would like to make a few modifications to the code with your help.

    The alpha change doesn't seem to happen until you click into the button, then out of the button. so they all start lit up, and then one by one they dim when clicking something else. How can I make it so that they start dim, and then light up on click?

    Also, instead of changing the alpha through code, how can I incorporate a gotoAndPlay("over2") or gotoAndPlay("out2") for that specific movieclip?

    Thanks again,
    SK

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To make them all start dim, dim them in the loop which adds the listener.

    You can simply replace the alpha code with your gotoAndPlay calls.

  7. #7
    Senior Member
    Join Date
    Oct 2005
    Posts
    157
    Hello, thanks for your help. This is my code so far, but there seems to be some problems. I'm sure I probably modified the code incorrectly, could you please help me out?

    Code:
    var buttons:Array = [box1, box2, box3];
    var current:MovieClip = null;
    
    for (var i:int = 0; i < buttons.length; i++){
      buttons[i].addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
    }
    
    function buttonPress(e:MouseEvent):void{
      if (current != null){
         current.gotoAndPlay("out2");
      }
      current = MovieClip(e.currentTarget);
      current.gotoAndPlay("over2");
    }

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What are the problems? You have not added any code in the loop as I suggested to initialize the state.

    If that's all it is, then try this:
    Code:
    var buttons:Array = [box1, box2, box3];
    var current:MovieClip = null;
    
    for (var i:int = 0; i < buttons.length; i++){
      var b:MovieClip = buttons[i];
      b.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      b.gotoAndPlay("out2");
    }
    
    function buttonPress(e:MouseEvent):void{
      if (current != null){
         current.gotoAndPlay("out2");
      }
      current = MovieClip(e.currentTarget);
      current.gotoAndPlay("over2");
    }

  9. #9
    Senior Member
    Join Date
    Oct 2005
    Posts
    157
    I'm sorry, what do you mean by the loop?

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    This part, the part I just changed:
    Code:
    for (var i:int = 0; i < buttons.length; i++){
      var b:MovieClip = buttons[i];
      b.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      b.gotoAndPlay("out2");
    }
    That iterates (or "loops") over the elements in the buttons array. I added the gotoAndPlay line in there to initialize all the buttons to be in the out state.

  11. #11
    Senior Member
    Join Date
    Oct 2005
    Posts
    157
    I tried implementing your code, and tried this:
    Code:
    import flash.display.*;
    import flash.events.*;
    
    
    var buttons:Array = [box1, box2, box3];
    var current:MovieClip = null;
    
    for (var i:int = 0; i < buttons.length; i++){
      buttons[i].addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      buttons[i].addEventListener(MouseEvent.MOUSE_DOWN, buttonPress2);
    }
    
    function buttonPress(e:MouseEvent):void{
      current = MovieClip(e.currentTarget);
      buttons[i].gotoAndPlay("clickthis");
      buttons[i].removeEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
    }
    
    
    function buttonPress2(e:MouseEvent):void{
      if (current != null){
         current.gotoAndPlay("out2");
      }
      current = MovieClip(e.currentTarget);
      current.gotoAndPlay("over2");
    }
    That code gave me an error:

    TypeError: Error #1010: A term is undefined and has no properties.
    at shahob_website8_fla::MainTimeline/buttonPress()


    Then I tried implementing the new code you gave me:
    Code:
    import flash.display.*;
    import flash.events.*;
    
    
    
    var buttons:Array = [box1, box2, box3];
    var current:MovieClip = null;
    
    for (var i:int = 0; i < buttons.length; i++){
      var b:MovieClip = buttons[i];
      b.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      b.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress2);
      b.gotoAndPlay("out2");
    }
    
    function buttonPress(e:MouseEvent):void{
      current = MovieClip(e.currentTarget);
      b.gotoAndPlay("clickthis");
      b.removeEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
    }
    
    function buttonPress2(e:MouseEvent):void{
      if (current != null){
         current.gotoAndPlay("out2");
      }
      current = MovieClip(e.currentTarget);
      current.gotoAndPlay("over2");
    }
    That one seemed as if it ignored all stop(); actions when I tested it out.

    I am trying to make my buttons so that when I first click any of them, they all slide down ("clickthis"). Then after that, they play "over2", but then play "out2" when another button is clicked.

    Thank you so much, hope to find a solution soon,
    SK

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In both versions of the code you just posted, you are attempting to refer to a movieclip with a reference that has been changed by the loop. In the first version, buttons[i] does not exist because the loop has already exited and i is now buttons.length. There's nothing in the array at that spot, so you get an undefined term error when attempting to access it.

    You have a very similar problem in the second version, but b IS still a valid reference. It just points to the last button. Which is not what you want.

    In both versions, you are assigning two listeners for the same event to the same button. When you do that, BOTH of those listeners will be called. Since you don't want buttonPress2 to happen on the first press, you should not add it as a listener to begin with. Instead, have buttonPress add buttonPress2 as a listener after it removes itself.

    I don't think you want the first press to set current, but I'm not sure about that.

    Try something like this:
    Code:
    import flash.display.*;
    import flash.events.*;
    
    var buttons:Array = [box1, box2, box3];
    var current:MovieClip = null;
    
    for (var i:int = 0; i < buttons.length; i++){
      var b:MovieClip = buttons[i];
      b.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      b.gotoAndPlay("out2");
    }
    
    function buttonPress(e:MouseEvent):void{
      //by declaring a variable here inside the function, it is a function local variable.  This is NOT the same b as that declared in the loop above, though it may refer to the same instance.
      var b:MovieClip = MovieClip(e.currentTarget); 
      b.gotoAndPlay("clickthis");
      b.removeEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      b.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress2);
    }
    
    function buttonPress2(e:MouseEvent):void{
      if (current != null){
         current.gotoAndPlay("out2");
      }
      current = MovieClip(e.currentTarget);
      current.gotoAndPlay("over2");
    }

  13. #13
    Senior Member
    Join Date
    Oct 2005
    Posts
    157
    Your code looks fine, but it seems to not work for some reason.
    I got what I wanted by modifying your code into this:
    Code:
    import flash.display.*;
    import flash.events.*;
    
    var buttons:Array = [box1, box2, box3];
    var current:MovieClip = null;
    
    box1.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
    box2.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
    box3.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
    
    function buttonPress(e:MouseEvent):void{
      box1.gotoAndPlay("clickthis");
      box2.gotoAndPlay("clickthis");
      box3.gotoAndPlay("clickthis");
      box1.removeEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      box2.removeEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      box3.removeEventListener(MouseEvent.MOUSE_DOWN, buttonPress);
      box1.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress2);
      box2.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress2);
      box3.addEventListener(MouseEvent.MOUSE_DOWN, buttonPress2);
    }
    
    function buttonPress2(e:MouseEvent):void{
      if (current != null){
         current.gotoAndPlay("out2");
      }
      current = MovieClip(e.currentTarget);
      current.gotoAndPlay("over2");
    }
    Still no idea why your code won't work as it seems to make sense to me.

    I have one problem though, not sure if you could help me with it, on my contact us page, I made a contact form, but for some reason I can't click in the input text boxes. I did embed the font already.

    Any ideas?

    Appreciate all your great help,
    SK

  14. #14
    Senior Member
    Join Date
    Oct 2005
    Posts
    157
    Oh one more question, why was b. pointing at my last button as you said before?

Tags for this Thread

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