Quantcast
Channel: Adobe Community : Discussion List - All Communities
Viewing all articles
Browse latest Browse all 238792

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

$
0
0

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


Viewing all articles
Browse latest Browse all 238792

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>