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
Thank you for your help, but the problem still persists. I changed the variable to a MovieClip, but it is still creating the same error. I am absolutely positive that my .fla is using the same ButtonGame class file. Could it be I am not correctly adding it to the stage? Or something wrong with the class itself?
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();
}
}
}
North America
Europe, Middle East and Africa
Asia Pacific