So I am putting together a school project, and is jsut starting, I am making a quiz in flash cc using as3. But when I compile my code I just get the error: TypeError: Error #1034: Type Coercion failed: cannot convert spr4$ to flash.display.MovieClip. Now I know the question has been asked, but I have not found any answers that works on my simple code here:
import flash.display.MovieClip;
import flash.events.MouseEvent;
stop();
var sprArray:Array = new Array();
sprArray[0] = [spr1, alt01, alt02, alt03];
sprArray[1] = [spr2, alt11, alt12, alt13];
sprArray[2] = [spr3, alt21, alt22, alt23];
sprArray[3] = [spr4, alt31, alt32, alt33];
btnNeste.addEventListener(MouseEvent.CLICK, neste);
function neste (evt:MouseEvent){
var randomSpr = Math.floor(Math.random()*4);
var spørsmål:MovieClip = sprArray[randomSpr][0];
spørsmål.x = 30;
spørsmål.width = 150;
spørsmål.height = 100;
var svaralt1:MovieClip = sprArray[randomSpr][1];
svaralt1.x = 30;
svaralt1.y = 50;
svaralt1.width = 100;
svaralt1.height = 100;
var svaralt2:MovieClip = sprArray[randomSpr][2];
svaralt1.x = 60;
svaralt1.y = 50;
svaralt1.width = 100;
svaralt1.height = 100;
var svaralt3:MovieClip = sprArray[randomSpr][3];
svaralt1.x = 90;
svaralt1.y = 50;
svaralt1.width = 100;
svaralt1.height = 100;
}
Your issue has to do with the nature of Classes and instances.
In your library, if you've checked the "export for actionscript" box and given a class name to that library object (let's say you gave one a class name of spr1
) then in your code, spr1
refers to a class (not an instance).
If you want an instance, you need to instantiate it from a class. So in your case, to get a new instance of spr1
, you would do:
new spr1();
So, to put that into context for your example:
//sprArray[x] refers to classes, so you need use the 'new' keyword to create new instance of that class
var spørsmål:MovieClip = new sprArray[randomSpr][0]();
Now, if you want to actually see that newly created display object, you have to give it a parent by using the addChild or addChildAt method:
addChild(spørsmål); //adds it to `this` (whatever display object this code is attached to) on top of everything else in `this`
Here is a question that has answers about the differences between classes and instances if you'd like to learn more.