-1

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()




}
3
  • In your code, you've got an array called 'elements' and one called 'allElements'. Look like they're supposed to be the same. Jul 6, 2009 at 13:30
  • Also, the var keyword needs to be inside the parens -- for (var i:uint...) Jul 6, 2009 at 13:38
  • right answer posted by oxmo ... however, it amazes me, your code runs ... this for example: Puzzle.polozaj.(container[i]) ... must be a typo of some kind or whatever ... greetz
    – back2dos
    Jul 6, 2009 at 14:21

7 Answers 7

1

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.

0

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.

0
0

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 ?

2
  • An array is untyped, so it is actually the proper thing to do. Jul 6, 2009 at 13:30
  • @blackberryoctopus -- yeah, bad start to the morning. couldn't correct my answer fast enough before someone saw it. :) Thanks for the correction. It would probably be better to stick those in the comments of the bad answer, in the future, to keep your answer from being confusing once the offending answer has been fixed. Jul 6, 2009 at 13:33
0

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?

0

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()
0

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!

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.