A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Button not working?

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    11

    Button not working?

    I can't believe this but I can't seem to get a button to work. I have tried movieClips and button symbols but nothing works. I have an initial frame that has a set of buttons and each button has a listener and function like so:

    btn_second_main.addEventListener(MouseEvent.MOUSE_ UP, eventHandler_main);
    function eventHandler_main(event:MouseEvent):void {
    if (event.target.name == "btn_second_main") {
    gotoAndStop(2);
    } else if (event.target.name == "btn_third_main") {
    gotoAndStop(3);
    } else {
    // do nothing
    }
    }

    And i have a frame that stretches from frame 2 to frame 6 that a different set of buttons and each one of them has a listener and a function like so:

    btn_second.addEventListener(MouseEvent.MOUSE_UP, btnHandler);
    function eventHandler(event:MouseEvent):void {
    if (event.target.name == "btn_second") {
    gotoAndStop(2);
    } else if (event.target.name == "btn_third") {
    gotoAndStop(3);
    } else {
    // do nothing
    }

    }

    ...every time i click on the first button on frame 1 and it takes me to frame 2 where the second set of buttons starts all of the second set of buttons work fine. But, when i click on any of the buttons on frame 1 (other than the first one) and it takes me to a frame other than just frame 2 none of the second set of buttons works.

    Please, please help. This is the most simple concept but I just can't seem to get it to work.

  2. #2
    Senior Member
    Join Date
    Aug 2007
    Posts
    291
    Can you attach your source file?

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    11
    Here is a link to the site (not working):
    http://www.cgmonster.com/test.html

    And here is a link to the file to download:
    www.cgmonster.com/test.fla


    Please let me know if you can figure it out.

  4. #4
    Senior Member
    Join Date
    Aug 2007
    Posts
    291
    I can't even test the file without the libraries you're using. I don't have "TweenMax". Have you tried tracing the button names from the 3rd frame to make sure they're registering correctly? Have you tried tracing the swfPathName to make sure it's correct for each button press?

    On a side note, you don't have to use "MOUSE_UP" on your events. You can use "MouseEvent.CLICK" and achieve better functionality.

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    11
    Well I'm not sure what you mean by tracing the "swfPathName" but I have tried tracing the event.target.name and it only works when I go to frame 2 and then go to another frame from there. On any other frame than the first to start, the buttons just dont respond at all. This is very confusing how it works sometimes and not others.

    FYI: If you delete everything on frame 5 of the "pages" layer (other than the buttons obviously) it should work and shouldn't ask you for TweenMax anymore. That is just a good tweening library that I downloaded.

    Please let me know if you can figure this out. This is SO frustrating.
    Last edited by Jr-; 06-18-2009 at 01:37 AM.

  6. #6
    Senior Member
    Join Date
    Aug 2007
    Posts
    291
    In the "frame 3d" movie clip, I just added a couple of trace statements to make sure they're working. There's a variable called "swfPathName". I traced it to make sure it had a valid url name in it. You'll see it below. Also, below the code, I'll paste what it's tracing and the errors I'm getting.

    PHP Code:
    stop();

    //////////////////////////////////////////////////////////////
    // Different Model Button Press Listeners ////////////////////
    //////////////////////////////////////////////////////////////

    // ButtonModes below omitted because buttons are not MovieClips
    // btn3d_cmon.buttonMode = true;
    btn3d_cmon.addEventListener(MouseEvent.MOUSE_UPbuttonPress);
    // btn3d_gpt.buttonMode = true;
    btn3d_gpt.addEventListener(MouseEvent.MOUSE_UPbuttonPress);
    // btn3d_guit.buttonMode = true;
    btn3d_guit.addEventListener(MouseEvent.MOUSE_UPbuttonPress);
    // btn3d_kart.buttonMode = true;
    btn3d_kart.addEventListener(MouseEvent.MOUSE_UPbuttonPress);
    // btn3d_lara.buttonMode = true;
    btn3d_lara.addEventListener(MouseEvent.MOUSE_UPbuttonPress);
    // btn3d_sw.buttonMode = true;
    btn3d_sw.addEventListener(MouseEvent.MOUSE_UPbuttonPress);
    // btn3d_wild.buttonMode = true;
    btn3d_wild.addEventListener(MouseEvent.MOUSE_UPbuttonPress);
    // btn3d_wc.buttonMode = true;
    btn3d_wc.addEventListener(MouseEvent.MOUSE_UPbuttonPress);


    // NetConnection object (for NetStream)
    var myNetConnection:NetConnection;
    // NetStream object
    var myNetStream:NetStream;
    // Sound object
    var mySound:Sound;
    // LocalConnection object
    var myLocalConnection:LocalConnection;

    // Create Loader to load movieClip into "holder"
    var loader:Loader = new Loader();


    //////////////////////////////////////////////////////////////
    // Load turn around into "holder" function ///////////////////
    //////////////////////////////////////////////////////////////

    function buttonPress(evt:MouseEvent):void {
        
    // Go to frame TWO
        
    gotoAndStop(2);
        
        
    // Unload Clip if not null
        
    if (loader.content != null) {
            
    ///////////////////////////////////////////////
            // http://www.youtube.com/watch?v=8hAIW0ppDww
            ///////////////////////////////////////////////
            //holder.removeChild(loader);
            //trace("not Null");
        
    }
        
        
    // Get Button Name onClick
        
    var btnName evt.target.name;
        
    // trace("Button Name: " + btnName);
        
        
    if (btnName != "") {
            
    // Remove prefix from button name
            
    trace("name: " btnName)
            var 
    btnNameTruncated btnName.substr(6btnName.length);
            
            
    // Set path and name of swf to load
            
    var swfPathName btnNameTruncated ".swf";
            
    trace(swfPathName);
        } else {
            
    trace("Button instance name is null");
        }
        
        
    holder.addChild(loader);
        
    loader.load(new URLRequest(swfPathName));

    I'm getting these errors because I don't have the SWFs to load.

    name: btn3d_cmon
    swfPathName: cmon.swf
    Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
    name: btn3d_guit
    swfPathName: guit.swf
    Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
    name: btn3d_lara
    swfPathName: lara.swf
    Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

  7. #7
    Junior Member
    Join Date
    Jun 2009
    Posts
    11
    Oh, that's for the 3d turn arounds that I have. I know that I am getting those errors. Even when i have those files to load (or not) I get the same results. The main navigation buttons (on frame 2+) dont work when I go from frame 1 to any frame other than 2.

    If I go from 1 to 2 everything works fine but even if I go from 1 to 2 then back to 1 and go to some frame other than 2 (from 1) the buttons just dont respond at all. Why would I need to go to the first frame of that set (2) for the buttons to work?

  8. #8
    Senior Member
    Join Date
    Aug 2007
    Posts
    291
    Oh, I get it now. For some reason, I wasn't getting the "main navigation" part. Here, take this code:

    PHP Code:

    btn_2d
    .addEventListener(MouseEvent.MOUSE_UPbtnHandler);
    btn_3d.addEventListener(MouseEvent.MOUSE_UPbtnHandler);
    btn_reel.addEventListener(MouseEvent.MOUSE_UPbtnHandler);
    btn_resume.addEventListener(MouseEvent.MOUSE_UPbtnHandler);
    btn_about.addEventListener(MouseEvent.MOUSE_UPbtnHandler); 
    And create new blank keyframes for each frame after 2 in your CODE layer and paste that code into each keyframe. You just have to redefine the listeners... you don't have to redefine the function. I think that should fix it. At least it fixed it for me. I couldn't do extensive testing because I couldn't pull in the external files it was calling, but the main navigation started working.

  9. #9
    Junior Member
    Join Date
    Jun 2009
    Posts
    11
    That seems a little reduntant doesn't it? Shouldn't it work somewhat similar to how I have it now though? I shouldn't have to redefine the listeners every frame should I? Or is there just a completely different way to do it maybe?

  10. #10
    Senior Member
    Join Date
    Aug 2007
    Posts
    291
    Hmmm... I'm not sure if you have to redefine a listener in each frame, but I've seen it done that way before. In either case, it fixed the problem.... for me anyway.

  11. #11
    Junior Member
    Join Date
    Jun 2009
    Posts
    11
    I dont know either. It just seems that I shouldn't have to redefine the listeners for each frame. It seems to me that it breaks the whole object oriented approach of not reusing code over and over. Obviously though I dont know how to get it to work without doing that so I'm stuck with the work around for now.

  12. #12
    Senior Member
    Join Date
    Aug 2007
    Posts
    291
    Quote Originally Posted by Jr- View Post
    I dont know either. It just seems that I shouldn't have to redefine the listeners for each frame. It seems to me that it breaks the whole object oriented approach of not reusing code over and over. Obviously though I dont know how to get it to work without doing that so I'm stuck with the work around for now.
    Actually, though it still may be the wrong way to do it, I think it fits in partly with the Object-Oriented approach. Though you have to reuse the listener code, you're not having to type all those functions over again...

  13. #13
    Junior Member
    Join Date
    Jun 2009
    Posts
    11
    Ok well, maybe I worded that incorrectly but it just seems that you should be able to put listeners on one key frame that spans multiple frames and the "listeners" should then listen for an event no matter what frame you're on in that set.

    The thing that is odd to me is that it "sort of" works that way but only if I go to the first frame in the set to start. If I go to the second frame or higher in that set and skip that first one, the buttons just dont do anything at all.

    By the way, thanks for all the help. I'm sure there is just one little thing that I'm missing.

  14. #14
    Senior Member
    Join Date
    Aug 2007
    Posts
    291
    I think I just realized why it does that.

    If you're skipping the second frame, it never actually runs that code. That code runs in the second frame whether you span that layer over multiple frames or not. So, if you skip that second frame (i.e. jump straight to the third frame), it's not going to run that code. You need to find a way to run that block of code regardless of what frame they go to. That's why you have to put it in each frame.

    Here's what I've tried so far to correct the problem:
    1. I just moved the block of code for the listeners into the first frame, but those buttons didn't exist yet (not until the 2nd frame) so it errored out.

    2. I completely removed the second frame of actions and just pasted the code I needed in the first frame. Of course, the same problem cropped up because those buttons don't exist until the second frame.

    So, if you're going to continue with this style of navigation, and you really want to avoid putting those listeners in each frame, you're going to have to find a way to run that block of code no matter which button is clicked, and it's going to have to be run while the buttons to which it refers are present on the stage.

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