6
votes

Solution:

if you have the same problem, addElement() instead of addChild() is what did it


I'm trying to move away from mxml to actionsctipt. I have a <s:Rect> that I've created and set its properties, but having trouble adding it.

var aRect:Rect = new Rect();
//set properties like aRect.x, aRect.y, aRect.width, aRect.height

//tried adding it various ways
addChild(aRect);
Application.addChild(aRect);
Application.application.addChild(aRect);
stage.addChild(aRect);

But I keep getting the error 1067: Implicit coercion of a value of type spark.primitives:Rect to an unrelated type flash.display:DisplayObject

Originally in the mxml, it was right inside <s:Application> not nested inside anything

<s:Application>

    <s:Rect id="aRect" x="10" y="10" width="15%" height="15%">
         //then fill code here, removed for readability
    </s:Rect>

</s:Application>

What's the deal, I thought actionscript would be nicer than mxml.

5
Never used Flex 4 before, but I'm curious about this. Hope you'll get an answer.Zed-K
Is your code example from the document class or any other class?JanD
@JanD, The code is right in the main mxml application (I have just one mxml, no components or anything), and of course the actionscript code is in an init() function in the script tag and gets triggered on creationComplete, pretty much exactly the usual stuff. Except that I'm now trying to create these visual elements with actionscript.touB

5 Answers

2
votes

tried changing addChild(aRect); to addElement(aRect); and that worked beautifully.

1
votes

It's because Flex 4 significantly changed the way the display hierarchy works in MXML-based applications. This is a bit confusing since addChild() no longer works as simply as you'd want it to - you have to add elements to a dataprovider, and then the logic of displaying those elements (which ones to add where, how to skin them, etc) is handled elsewhere. It's kind of a useful change, though, because it forces you separate your concerns in a very concrete way. Once you have your elements all added to your dataProvider you can swap out Layout objects at will (even at runtime) to change the way your application looks.

EDIT: Technically it's not the displayList itself that they've changed. It's the fact that the basic unit used by Flex is now the "Group" - even s:Application extends group. You add your content to a a Group (or to the top level Application) and then you assign the group a layout to tell it how to display the items you've added.

0
votes

Yes, you need a DisplayObject. I'm not familiar with spark.primitives.Rect, but perhaps you could just create a new Sprite and call methods on its Graphics object to draw the rectangle?

0
votes

According to the live docs, the addChild method of the Application class does require it to be a displayObject.

0
votes

Annoyingly we will often struggle to add flash assets ( swf swc ) (display objects) using addElement.

I'm working on a way to do this right now :( more hoops and jumping

Also my swc is not viewable in the package explorer (why not ?)