Highlighted

_root to AS3 - EASY ONE!

Contributor ,
Nov 15, 2010

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!

TOPICS
ActionScript

Views

23.7K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

_root to AS3 - EASY ONE!

Contributor ,
Nov 15, 2010

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!

TOPICS
ActionScript

Views

23.7K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 15, 2010 0
Participant ,
Nov 15, 2010

Copy link to clipboard

Copied

Hi,

   In ActionScript 3 _root was changed to just root

Hope this helps,

Regards,

Peter Witham

www.uibuzz.com

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 0
Contributor ,
Nov 15, 2010

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 0
LEGEND ,
Nov 15, 2010

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);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 2
LEGEND ,
Nov 15, 2010

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 0
Participant ,
Nov 15, 2010

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 1
LEGEND ,
Nov 15, 2010

Copy link to clipboard

Copied

Peter, I do agree with your suggestion to cast to Object for scalability sake.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 0
Contributor ,
Nov 15, 2010

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???

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 0
LEGEND ,
Nov 15, 2010

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);
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 1
Participant ,
Nov 15, 2010

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 0
Contributor ,
Nov 15, 2010

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!  😕

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 0
Participant ,
Nov 15, 2010

Copy link to clipboard

Copied

Excellent, glad you got it to work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2010 0
Contributor ,
Nov 16, 2010

Copy link to clipboard

Copied

Yes - thank you all for your help! 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 16, 2010 0
New Here ,
Sep 13, 2015

Copy link to clipboard

Copied

thanks, glad you got it to work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 13, 2015 0
DAFSKY LATEST
New Here ,
Mar 26, 2020

Copy link to clipboard

Copied

mute_btn.addEventListener(MouseEvent.CLICK, onMute);

// try to delete _root.
function onMute(e:MouseEvent):void {

     soundbars_mc.gotoAndStop(2);

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 26, 2020 0