Copy link to clipboard
Copied
I swear I've googled all over for this before I posted. I've tried things I've read, but nothing's worked (or made sense to me, for that matter).
What I have is pretty basic. Trying to do what WOULD have been this in AS2:
mute_btn.addEventListener(MouseEvent.CLICK, onMute);
function onMute(e:MouseEvent):void {
_root.soundbars_mc.gotoAndStop(2);
}
Both mute_btn and soundbars_mc are on the main timeline. I can't get this to work, and I know it's easy. Any help???
Thanks!
Copy link to clipboard
Copied
Hi,
In ActionScript 3 _root was changed to just root
Hope this helps,
Regards,
Peter Witham
www.uibuzz.com
Copy link to clipboard
Copied
That's what I thought. But if I make that line:
root.soundbars_mc.gotoAndStop(2);
Then I get this error:
Scene 1, Layer 'Actions', Frame 1, Line 76 1119: Access of possibly undefined property soundbars_mc through a reference with static type flash.display:DisplayObject.
Copy link to clipboard
Copied
AS3 gets picky (aka stupid) at times, requiring you to tell it what it already knows anyways. Try using...
MovieClip(root).soundbars_mc.gotoAndStop(2);
Copy link to clipboard
Copied
"requiring you to tell it what it already knows anyways."
As a matter of fact root can be many things including Sprite and, most importantly, custom datatypes as long as they extend DisplayObject. Yes, in the majority of cases it is MovieClip because developers usually settle for MovieClip default and because MovieClip is a dynamic class. If root is something else (not MovieClip) casting MovieClip(root) will not work.
Copy link to clipboard
Copied
Was going to suggest
mute_btn.addEventListener(MouseEvent.CLICK, onMute);
function onMute(e:MouseEvent):void {
Object(root).soundbars_mc.gotoAndStop(2);
}
Which would do the samething but using the generic object.
Copy link to clipboard
Copied
Peter, I do agree with your suggestion to cast to Object for scalability sake.
Copy link to clipboard
Copied
I tried both:
Object(root).soundbars_mc.gotoAndStop(2);
and
MovieClip(root).soundbars_mc.gotoAndStop(2);
Both get the same error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at player_fla::MainTimeline/onMute()
Sorry this is so complicated. I thought it'd be a simple one! 😕
Any other suggestions???
Copy link to clipboard
Copied
You said that both the button and sound_bar are on the same timeline. So, why do you need the reference to root? root can change.
Does the following work?
mute_btn.addEventListener(MouseEvent.CLICK, onMute);
function onMute(e:MouseEvent):void {
soundbars_mc.gotoAndStop(2);
}
Copy link to clipboard
Copied
I'm a dope. It was other code on the page making it screwy. It works fine with just the soundbars_mc.gotoAndStop(2). My apologies! 😕
Copy link to clipboard
Copied
Excellent, glad you got it to work.
Copy link to clipboard
Copied
Yes - thank you all for your help!
Copy link to clipboard
Copied
I put the code on the root of a flash file with a button and movieclip with the same instance names and it worked just fine for me so you may have something else complicating the code maybe?
Copy link to clipboard
Copied
thanks, glad you got it to work.
Copy link to clipboard
Copied
mute_btn.addEventListener(MouseEvent.CLICK, onMute);
// try to delete _root.
function onMute(e:MouseEvent):void {
soundbars_mc.gotoAndStop(2);
}