开发者

Flash Buttons Don't Work: TypeError: Error #1009: Cannot access a property or method of a null object reference

I've read through several threads about this error, but haven't been able to apply it to figure out my situation...

My flash file is an approx 5 second animation. Then, the last keyframe of each layer (frame #133) has a button in it. My flash file should stop on this last key frame, and you should be able to click on any of the 6 buttons to navigate to another html page in my website.

Here is the Action Script that I have applied to the frame in which the buttons exist (on a separate layer, see sc开发者_StackOverflowreenshot at: http://www.footprintsfamilyphoto.com/wp-content/themes/Footprints/images/flash_buttonissue.jpg

stop ();


function babieschildren(event:MouseEvent):void 
{ 
    trace("babies children method was called!!!");
    var targetURL:URLRequest = new URLRequest("http://www.footprintsfamilyphoto.com/portfolio/babies-children"); 
    navigateToURL(targetURL, "_self"); 
}

bc_btn1.addEventListener(MouseEvent.CLICK, babieschildren);
bc_btn2.addEventListener(MouseEvent.CLICK, babieschildren);


function fams(event:MouseEvent):void 
{ 
    trace("families method was called!!!");
    var targetURL:URLRequest = new URLRequest("http://www.footprintsfamilyphoto.com/portfolio/families"); 
    navigateToURL(targetURL, "_self"); 
}

f_btn1.addEventListener(MouseEvent.CLICK, fams);
f_btn2.addEventListener(MouseEvent.CLICK, fams);


function couplesweddings(event:MouseEvent):void 
{ 
    trace("couples weddings method was called!!!");
    var targetURL:URLRequest = new URLRequest("http://www.footprintsfamilyphoto.com/portfolio/couples-weddings"); 
    navigateToURL(targetURL, "_self"); 
}

cw_btn1.addEventListener(MouseEvent.CLICK, couplesweddings);
cw_btn2.addEventListener(MouseEvent.CLICK, couplesweddings);

When I test the movie, I get this error in the output box:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

The test movie does stop on the appropriate frame, but the buttons don't do anything (no URL is opened, and the trace statements don't show up in the output box when the buttons are clicked on the test movie).

You can view the .swf file here: www.footprintsfamilyphoto.com/portfolio

I'm confident that all 6 buttons do exist in the appropriate frame (frame 133), so I don't think that's what's causing the 1009 error.

I also tried deleting each of the three function/addEventListener sections one at a time and testing, and I still got the 1009 error every time. If I delete ALL of the action script except for the "stop ()" line, then I do NOT get the 1009 error.

Any ideas?? I'm very new to Flash, so if I haven't clarified something that I need to, let me know!


Update: I'm getting the feeling that this has something to do with the construction of my file rather than the code itself - if anyone has suggestions for more screenshots/information I could include here that might help to reveal any constructional flaws, let me know, and I'll be glad to capture/post them. I'm just not sure what to be looking for as the source of Error 1009? I've confirmed and reconfirmed my instance names... all buttons exist in the same frame where the action script is located (frame 133). i'm not importing any external objects...

Any suggestions would be much appreciated!


I had the exact same error and I read here something about the swf not catching up to the button instance on time even though it's on the same frame and it was exaclty that.

I simply extended the button 1 frame before the actionscript and worked!


i rebuild your sample using CS3 and at least for me it works as expected and your code looks correct as well.. the only thing i could imagine is that there is not an instance-name (e.g. "bc_btn1") on EVERY KEYFRAME in your timeline.

in this case i'm getting the same error.. so maybe you should check that.

regards


Thanks so much for your response! If I had a copy of CS3, I'd be more than willing to revert, given the headaches CS4 has been giving me!

I ended up sending my file over to a friend/flash guru to fix directly. Pasting his response here in case it's helpful to anyone else!

To be honest, I'm a little suprised it's doing this to you, especially since I can run the program from the Flash authoring environment without problem - it's only when you actually launch the swf externally that the problem happens.

You were definitely on the right track with checking those button instances. It seems as if because the buttons are added to that frame, at about the same time the frame script is supposed to run, it's just catching itself out of order. So it's trying to call addEventListener on the buttons before the program knows they exist.

I can't say whether my solution is the correct one, but it should work and be relatively quick. When the program reaches that frame, I'm having it check to make sure each of those buttons exists. I put this check inside a function that gets called by the ENTER_FRAME event, which will happen continuously at your program's framerate as long as the program is running (this is independent of the stop() command we're calling). As soon as the check passes, meaning that none of the buttons are null, I add the event listeners for all of the buttons and remove the listener for the ENTER_FRAME so that this checker method stops running.

Here's the code I have for that frame now. See if this helps - it works for me now. I'm going to have to read up on this a little more now to see what techniques are recommended for dealing with this.

import flash.events.Event;

stop ();

//listen for the Flash player's ENTER_FRAME event...
this.addEventListener(Event.ENTER_FRAME, onEnterFrame, false);

//and call this checker function continually until all of the buttons are accounted for
function onEnterFrame(e:Event):void {
 if (bc_btn1 != null && bc_btn2 != null && f_btn1 != null && f_btn2 != null && cw_btn1 != null && cw_btn2 != null){
  bc_btn1.addEventListener(MouseEvent.CLICK, babieschildren);
  bc_btn2.addEventListener(MouseEvent.CLICK, babieschildren);

  f_btn1.addEventListener(MouseEvent.CLICK, fams);
  f_btn2.addEventListener(MouseEvent.CLICK, fams);

  cw_btn1.addEventListener(MouseEvent.CLICK, couplesweddings);
  cw_btn2.addEventListener(MouseEvent.CLICK, couplesweddings);

  //clean up the enter frame listener so that this function no longer gets called
  this.removeEventListener(Event.ENTER_FRAME, onEnterFrame, false);
 }
}



function babieschildren(event:MouseEvent):void 
{ 
    trace("babies children method was called!!!");
    var targetURL:URLRequest = new URLRequest("http://www.footprintsfamilyphoto.com/portfolio/babies-children"); 
    navigateToURL(targetURL, "_self"); 
}


function fams(event:MouseEvent):void 
{ 
    trace("families method was called!!!");
    var targetURL:URLRequest = new URLRequest("http://www.footprintsfamilyphoto.com/portfolio/families"); 
    navigateToURL(targetURL, "_self"); 
}


function couplesweddings(event:MouseEvent):void 
{ 
    trace("couples weddings method was called!!!");
    var targetURL:URLRequest = new URLRequest("http://www.footprintsfamilyphoto.com/portfolio/couples-weddings"); 
    navigateToURL(targetURL, "_self"); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜