Question

I have these Arrays

   //Array elements are Sprites (Class) in Flash Library

    var elements:Array  = new Array (el1_spr, el2_spr, el3_spr);
    var container:Array = new Array();

    for var (i:uint; allElements.length; i++){
       container.push(allElements[i]);
       var v:Sprite = (allElements[i] as Sprite);

    addChild(container[i]);
    Puzzle.polozaj.(container[i]);

//Error is:
//TypeError: Error #1034: Type Coercion failed: cannot convert el1_spr$ to flash.display.DisplayObject.
    at project_fla::MainTimeline/frame1()




}
Was it helpful?

Solution

Looks like you could probably add v to the display list, rather than container[i]. Have you tried that?

UPDATE: Actually, I think the problem comes from the second item in your loop definition. It should be i < allElements.length, rather than allElements.length. The way you've got it set up, it is running one time more than it should.

Below is some working code, based on what you posted, above. Hit me with a comment if I need to clarify (anyone - been a rough morning, so far...).

var el1_spr:Sprite = new Sprite;
var el2_spr:Sprite = new Sprite;
var el3_spr:Sprite = new Sprite;

el1_spr.graphics.beginFill(0x0000FF);
el1_spr.graphics.moveTo(0,0);
el1_spr.graphics.lineTo(100,0);
el1_spr.graphics.lineTo(100,100);
el1_spr.graphics.lineTo(0,100);
el1_spr.graphics.lineTo(0,0);
el1_spr.graphics.endFill();

el2_spr.graphics.beginFill(0x00FF00);
el2_spr.graphics.moveTo(0,0);
el2_spr.graphics.lineTo(100,0);
el2_spr.graphics.lineTo(100,100);
el2_spr.graphics.lineTo(0,100);
el2_spr.graphics.lineTo(0,0);
el2_spr.graphics.endFill();

el3_spr.graphics.beginFill(0xFF0000);
el3_spr.graphics.moveTo(0,0);
el3_spr.graphics.lineTo(100,0);
el3_spr.graphics.lineTo(100,100);
el3_spr.graphics.lineTo(0,100);
el3_spr.graphics.lineTo(0,0);
el3_spr.graphics.endFill();

var elements:Array  = new Array (el1_spr, el2_spr, el3_spr);
var container:Array = new Array();

for (var i:uint; i < elements.length; i++)
{
    container.push(elements[i]);
    var v:Sprite = (elements[i] as Sprite);

    addChild(v);
    //addChild(elements[i]);        // this also works
    //addChild(container[i]);       // this also works

    v.x += (100 * i);
    //elements[i].x += (100 * i);   // this also works
    //container[i].x += (100 * i);  // this also works
}

I don't know what the context of your code is, but for what it might be worth, I included a couple of extra lines to show that you don't necessarily need the container Array, or to create v.

Hope that's helpful.

OTHER TIPS

el1_spr is a Class object not a Sprite

You should do:

var s:Sprite = new el1_spr() as Sprite;

in order to get your Sprite object.

Sprite DOES inherit from DisplayObject.

The inheritance chain looks like this :

Sprite -> DisplayObjectContainer -> InteractiveObject -> DisplayObject

My question about your code is this :

Why are you casting an object that is already a Sprite, to Sprite ?

You start by declaring elements but then later you use allElements. Is it suposed to be like that?

You set your local variable v but never use it, when you cast with the as operator null is returned if the cast is not posible. So you would not get an error here.

For me this looks like allElements contains none DisplayObject elements.

Also the line Puzzle.polozaj.(container[i]);, looks a bit odd. Maybe you have a dot to much after polozaj?

Thank you for help. I've cleared code and context here.

values in the elements Array are objects from the Flash Library el1_spr, el2_spr, el3_spr are the Class Names in the Symbol Properties. Base Class of Symbols is flash.display.Sprite(Symbol Properties) I need to add this Library Sprites to the stage and process them with Puzzle.polozaj(Class, Method).

var elements:Array = new Array (el1_spr, el2_spr, el3_spr);

for (var i:uint; i < elements.length; i++){

    addChild(elements[i]);
    Puzzle.polozaj(elements[i]);

}
//TypeError: Error #1034: Type Coercion failed: cannot convert a1_spr$ to flash.display.DisplayObject.
    at puzzle_fla::MainTimeline/frame1()

I solved the problem. I just needed to make new instances of the symbols, and put variable names as reference in the Array.

Is there an option where you tag thread as solved?

Great site btw!

Generally it's a much better idea to use Array literals instead of a constructor. Makes quite a difference in AS3: ActionScript 3 Array constructor vs Array literals benchmark

Alternatively of course, given the above case, using Vector would be even better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top