Skip navigation
ZXZ1661
Currently Being Moderated

How come I can't convert the root variable to a MovieClip?

Aug 14, 2012 8:09 PM

Tags: #problem #script #as3 #button #convert #movieclip #stage

In one of my custom classes for my program I am trying to access the main timeline from within it. The following is code which I have used before, but for some reason does not seem to be working now. The full error I get (During runtime, not during compiling) is

[code]

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@28132041 to flash.display.MovieClip.

    at ButtonGame/onAddedToStage()

    at flash.display::DisplayObjectContainer/addChild()

    at flash.display::Stage/addChild()

    at RECOVER_CoinGame_fla::MainTimeline/frame2()

[/code]

 

 

And my code is:

[code]

package  {

    import flash.display.MovieClip;

    import flash.events.Event;

    import flash.media.Sound;

    import flash.events.MouseEvent;

    public class ButtonGame extends MovieClip

    {        private var _root:Object;

        private var buttonSound1:Sound;       

 

        public function ButtonGame(btnX:int, btnY:int, btnWidth:int, btnHeight:int, btnString:String)

        {

            addEventListener(MouseEvent.CLICK, clickHandler);

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

        }

 

        function onAddedToStage(e:Event):void

        {

            _root = MovieClip(root); //Code the error is referencing to.

            buttonSound1 = _root._resource.CoinSound;

            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

        }

 

        private function clickHandler(e:MouseEvent):void

        {

            buttonSound1.play();

        }

 

    }

 

}

[/code]

And this same exact code worked in a different class, but for some reason it doesn't seem to work for this.

Please help i've been stuck on this for about an hour, trying many different things and searching the internet.

 

-Thanks

 
Replies
  • kglad
    62,172 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 14, 2012 9:38 PM   in reply to ZXZ1661

    use

     

    private var _root:MovieClip;

     

    but casting as an object wouldn't cause that error.  your fla must be using some other ButtonGame class file.

     
    |
    Mark as:
  • kglad
    62,172 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 15, 2012 8:39 AM   in reply to ZXZ1661

    as long as you're using the added_to_stage event, that's not the problem.

     

    do you see any trace output when using:

     

     

    package  {

        import flash.display.MovieClip;

        import flash.events.Event;

        import flash.media.Sound;

        import flash.events.MouseEvent;

        public class ButtonGame extends MovieClip

        {        private var _root:Object;

            private var buttonSound1:Sound;       

     

            public function ButtonGame(btnX:int, btnY:int, btnWidth:int, btnHeight:int, btnString:String)

            {

                addEventListener(MouseEvent.CLICK, clickHandler);

                addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            }

     

            function onAddedToStage(e:Event):void

            {

    trace(this);

    trace(root);

                _root = MovieClip(root); //Code the error is referencing to.

                buttonSound1 = _root._resource.CoinSound;

                removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            }

     

            private function clickHandler(e:MouseEvent):void

            {

                buttonSound1.play();

            }

     

        }

     

    }

     
    |
    Mark as:
  • kglad
    62,172 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 16, 2012 7:00 AM   in reply to ZXZ1661

    alright.

     

    the problem is you're adding your ButtonGame instance to the stage:

     

    stage.addChild(yourButtonGameinstance);

     

    in that situation the top-most display object for your ButtonGame instance is the stage.

     
    |
    Mark as:
  • kglad
    62,172 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 16, 2012 12:00 PM   in reply to ZXZ1661

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points