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

_root to AS3 - EASY ONE!

Contributor ,
Nov 15, 2010 Nov 15, 2010

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
25.6K
Translate
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

Hi,

   In ActionScript 3 _root was changed to just root

Hope this helps,

Regards,

Peter Witham

www.uibuzz.com

Translate
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

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.

Translate
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

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

Translate
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

"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.

Translate
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

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.

Translate
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

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

Translate
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

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

Translate
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

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

Translate
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

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

Translate
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

Excellent, glad you got it to work.

Translate
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

Yes - thank you all for your help! 

Translate
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

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?

Translate
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

thanks, glad you got it to work.

Translate
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
LATEST

mute_btn.addEventListener(MouseEvent.CLICK, onMute);

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

     soundbars_mc.gotoAndStop(2);

}

Translate
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