Copy link to clipboard
Copied
So, what I'm trying to do is build a simple page where from the first page with buttons. You click a button and it fires off a simple gotoandstop function like this:
stop();
l2v2_btn.addEventListener(MouseEvent.CLICK, l2v2);
function l2v2(event:MouseEvent):void
{
gotoAndStop(2);
}
From there I want each page to have a bunch of buttons, each button has a sound that plays when you hit it. Wonder, works, easy, swell.
The problem arises when I try to attach a function to those buttons. I want each button press to add a child, or remove a child. Now...when I just do it all on frame 1 - no problem, but when I go from frame 1 (main menu to any other frame (at this point frame 2) I get this, an error that I'm so sick of I can puke.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at shit_fla::MainTimeline/frame2()
at flash.display::MovieClip/gotoAndStop()
at shit_fla::MainTimeline/l2v2()
actions on frame 1 are printed above
code on frame 2 ate here:
var nd:And = new And();
var big:Big = new Big();
var current = "none";
and_btn.addEventListener(MouseEvent.CLICK, fAnd);
big_btn.addEventListener(MouseEvent.CLICK, fBig);
function fBig(event:MouseEvent):void
{
if(current == "none")
{
addChild(nd);
current = nd.name;
}
else if(current != "none")
{
removeChild(getChildByName(current));
addChild(nd);
nd.x = 818;
nd.y = 438;
current = nd.name;
}
}
function fAnd(event:MouseEvent):void
{
if(current == "none")
{
addChild(big);
current = big.name;
}
else if(current != "none")
{
removeChild(getChildByName(current));
addChild(big);
current = big.name;
}
}
Basically I just want a main page, I click a button takes me to another page with buttons, each button plays a vocabulary audio and adds a sprite (vocab word to the stage) when
It works when I'm on frame one, the swapping out the sprites, but when I try to go another frame with the same it get the aforementioned error. I know this is a simple fix, could someone please help? This is driving me bonkers
Actually this seemed to fix my problem:
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(and_btn != null && big_btn != null )
{
and_btn.addEventListener(MouseEvent.CLICK, fAnd);
big_btn.addEventListener(MouseEvent.CLICK, fBig);
//clean up the enter frame listener so that this
...Copy link to clipboard
Copied
click file>publish settings>and tick 'permit debugging'>retest.
the line number that's trying to reference a non existant object will be in the error message. if that doesn't solve your problem, copy and paste the problematic line of code in your next message.
p.s. you might want to read about the 1009 error here, Debugging ActionScript 3.0 Errors
Copy link to clipboard
Copied
Thanks, I did what you suggested, and still got the same error. I haven't used flash in years (like 10) so rusty is beyond how I am, not to mention I was never adept at it. That said, when I checked the permit=-debugging and re-ran, nothing new happened it still spit out:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at TesT_2_fla::MainTimeline/frame2()[TesT_2_fla.MainTimeline::frame2:6]
This isn't coming up as an error though.
These seem to be the offending lines (since the program runs, doesn't work of course, but runs without flash complaining) when I comment them out
and_btn.addEventListener(MouseEvent.CLICK, fAnd);
big_btn.addEventListener(MouseEvent.CLICK, fBig);
I have nothing on the frame 1 stage, the only code I have is gotoAndStop(2) - I've tried running it without this code and same problem
frame 2 I have the two buttons and the second batch of code above.
The thing is when I have it all on frame run, it runs without a hitch.
What's happening (at least it seems to me) is the play head gets to frame 2, runs the code before the stage is populated and doesn't see the buttons and balks, is there any way around this?
Thanks for your reply and any further help you may be able to offer.
I' suprised I haven't seen anyone else with this problem, it just seems like such a noobie mistake is being made somewhere.
Copy link to clipboard
Copied
the error is on line 6.
if that references and_btn, it means and_btn doesn't exist when that line of code executes. likewise, if line 6 references big_btn.
Copy link to clipboard
Copied
Yeah, I get that. The thing is, when the buttons and code are on frame 1 - it works fine. When frame 1 is empty - and the buttons and code are on frame 2, it's not seeing the buttons.
It seems like it's processing the script before it sees the stage in frame 2, which is confusing because the script is also on frame 2.
Copy link to clipboard
Copied
start a new fla.
add a keyframe to frame 2.
add something to the stage and convert it to a button or movieclip and assign it an instance name.
open the actions panel and reference that instance name is some actionscript that's in frame 2.
any problem?
Copy link to clipboard
Copied
Actually this seemed to fix my problem:
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(and_btn != null && big_btn != null )
{
and_btn.addEventListener(MouseEvent.CLICK, fAnd);
big_btn.addEventListener(MouseEvent.CLICK, fBig);
//clean up the enter frame listener so that this function no longer gets called
this.removeEventListener(Event.ENTER_FRAME, onEnterFrame, false);
}
}
Of course now I don't know how to add sprites to the stage at an initial position other than 0,0.
Copy link to clipboard
Copied
that code would only be needed if it's added to a frame that plays BEFORE the frame that contains your buttons.
if you use code to add an object to the stage, it's always added at 0,0
you can then assign the object's x and y properties to change where it appears on stage:
var dobj:YourDisplayObjectClass=new YourDisplayObjectClass();
addChild(dobj); // added to 0,0
dobj.x=100;
dobj.y=200;
// dobj is now at 100,200 and the stage isn't updated until all code in this frame executes so users won't see the change from 0,0 to 100,200
Copy link to clipboard
Copied
Yeah, I know...I'm puzzled why I even need it. Where I got that code was from somone with a similar problem. For some reason flash was processing the AS as soon as it hits the frame, and so quickly it happened before buttons on the frame where there. I dunno. But thanks for all your help!!!!
Thanks for the x,y tip!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now