• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

_root to AS3 - EASY ONE!

Contributor ,
Nov 15, 2010 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

25.0K

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
community guidelines
Participant ,
Nov 15, 2010 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

Votes

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
community guidelines
Contributor ,
Nov 15, 2010 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.

Votes

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
community guidelines
LEGEND ,
Nov 15, 2010 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);

Votes

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
community guidelines
LEGEND ,
Nov 15, 2010 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.

Votes

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
community guidelines
Participant ,
Nov 15, 2010 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.

Votes

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
community guidelines
LEGEND ,
Nov 15, 2010 Nov 15, 2010

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Contributor ,
Nov 15, 2010 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???

Votes

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
community guidelines
LEGEND ,
Nov 15, 2010 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);
}

Votes

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
community guidelines
Contributor ,
Nov 15, 2010 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!  😕

Votes

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
community guidelines
Participant ,
Nov 15, 2010 Nov 15, 2010

Copy link to clipboard

Copied

Excellent, glad you got it to work.

Votes

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
community guidelines
Contributor ,
Nov 16, 2010 Nov 16, 2010

Copy link to clipboard

Copied

Yes - thank you all for your help! 

Votes

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
community guidelines
Participant ,
Nov 15, 2010 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?

Votes

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
community guidelines
New Here ,
Sep 13, 2015 Sep 13, 2015

Copy link to clipboard

Copied

thanks, glad you got it to work.

Votes

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
community guidelines
New Here ,
Mar 26, 2020 Mar 26, 2020

Copy link to clipboard

Copied

LATEST

mute_btn.addEventListener(MouseEvent.CLICK, onMute);

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

     soundbars_mc.gotoAndStop(2);

}

Votes

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
community guidelines