2

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;

}
TubbyS
  • 21
  • 2
  • The error is because you're trying to store a reference to something that isn't a `MovieClip` into a `MovieClip` var. Are you sure all the items in your arrays are MovieClips? You could easily change your vars to be `DisplayObjects` since all you're doing is setting width/height and position – BadFeelingAboutThis Jun 06 '16 at 15:40
  • Could you give me a quick rundown on how to do this? As you understand I am not that great at as3. Thank you so much for your help! – TubbyS Jun 06 '16 at 16:14
  • the sprx and altxx objects should be movie clips. I made them using the insert menu, having movie clip type selected and later adding linkage to sprx and altxx. – TubbyS Jun 06 '16 at 16:41
  • Do you have items on your timeline with the instance name `spr1` etc? Or are you trying to create new instances of that library object with your code above? – BadFeelingAboutThis Jun 06 '16 at 16:55
  • I am trying to create new instances of that library object. – TubbyS Jun 06 '16 at 16:58
  • Ok, I can answer your question now. – BadFeelingAboutThis Jun 06 '16 at 16:58

1 Answers1

0

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.

Community
  • 1
  • 1
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • So this removed the error, which I take as a sign that it worked, but now how do i make the movieclip show up on the scene? For some reason this does not happen. – TubbyS Jun 06 '16 at 17:19
  • Nevermind. If you are reading this later and is stupid like me: use addChild(yourMoviecliphere); tomake the object show up to stage. Thank you so much for your help BadFelling! – TubbyS Jun 06 '16 at 17:52