views:

581

answers:

5

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.

A: 

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?

aib
A: 

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

invertedSpear
+1  A: 

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

touB
+1  A: 

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.

Myk
Myk are you interested in making a blog post about that and maybe adding it to your answer here. The part about "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." is especially interesting and is probably better understandable with code than words. Thanks.
touB
Ha, I suppose someone should probably write that up - I know that the Flex 4 Cookbook from O'Reilly press does a really good job of explaining it. That should be out soon. In the meantime I'll look around for a good writeup, I'm sure there's something out there - if I find it I'll post it here.
Myk
Here's a video tutorial explaining it - http://flex4.org/2009/06/23/flex-4-layouts/Basically, most Flex 4 views (including s:Application) have a layout property. That determines how objects are displayed, and you can create your own custom layouts in addition to the four basic ones. The video explains it pretty well!
Myk
A: 

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

landed