What I’m trying to do below in AS3 is something I’ve done in AS2 so many times I could do it drunk in the dark.
Here’s the deal. I have a bunch of buttons on the stage. Those are in an array and I’ve gotten them to work just fine as far as mousing over and out and passing the i value to a function.
I have a bunch of movieclips in the library that have been given instance names through linkage. Those movieclip names have been painstakingly, lovingly entered into another array.
When I click a button I want the corresponding movieclip to be added to the stage with addChild. I keep getting an error that seems to indicate that that the movieclip listed in the array is the wrong “type”. The highly offensive error message is below:
TypeError: Error #1034: Type Coercion failed: cannot convert myMovieClip$ to flash.display.MovieClip
I’ve used addChild many times, but not hand and hand with arrays.
The infuriating code that won’t work is below:
var aClipsToAttach:Array = new Array(mcAlta, mcAncil, mcAntelope, mcApple, mcAuburn, mcBartley, mcBass, mcBing, mcCastle, mcCatta, mcCherry, mcCordova, mcDarkhorse, mcDavis, mcDiamond, mcDrycreek, mcEmpire, mcHaggin, mcLacontenta, mcLincoln, mcLockeford, mcMather, mcMorgan, mcPlumas, mcRidge, mcRiovista, mcRiveroaks, mcSunset, mcTeal, mcTimber, mcTrinitas, mcTurkey, mcWhitney, mcWildhawk, mcWildhorse, mcWinchester, mcWoodcreek, mcYocha);
var aButtons:Array = new Array(btnAlta, btnAncil, btnAntelope, btnApple, btnAuburn, btnBartley, btnBass, btnBing, btnCastle, btnCatta, btnCherry, btnCordova, btnDarkhorse, btnDavis, btnDiamond, btnDrycreek, btnEmpire, btnHaggin, btnLacontenta, btnLincoln, btnLockeford, btnMather, btnMorgan, btnPlumas, btnRidge, btnRiovista, btnRiveroaks, btnSunset, btnTeal, btnTimber, btnTrinitas, btnTurkey, btnWhitney, btnWildhawk, btnWildhorse, btnWinchester, btnWoodcreek, btnYocha);
for (var i:Number = 0; i < aButtons.length; i++) {
aButtons*.buttonMode = true;
aButtons*.addEventListener(MouseEvent.CLICK, clickHandler);
aButtons*.addEventListener(MouseEvent.ROLL_OVER, overClip);
aButtons*.addEventListener(MouseEvent.ROLL_OUT, outClip);
}
function clickHandler(evt:MouseEvent):void {
for (var i:Number = 0; i < aButtons.length; i++) {
if (evt.currentTarget == aButtons*) {
clipOpen(i);
}
}
}
function overClip(evt:MouseEvent):void {
for (var i:Number = 0; i < aButtons.length; i++) {
if (evt.currentTarget == aButtons*) {
aButtons*.gotoAndStop(“over”);
}
}
}
function outClip(evt:MouseEvent):void {
for (var i:Number = 0; i < aButtons.length; i++) {
if (evt.currentTarget == aButtons*) {
aButtons*.gotoAndStop(“out”);;
}
}
}
function clipOpen(num){
trace(aClipsToAttach[num]);
var moveClip:MovieClip=aClipsToAttach[num];
this.addChild(moveClip);
}