does actionscript addChild require a display object first
Asked Answered
H

5

6

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.

Homozygous answered 19/3, 2010 at 16:49 Comment(3)
Never used Flex 4 before, but I'm curious about this. Hope you'll get an answer.Bethany
Is your code example from the document class or any other class?Ivanaivanah
@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.Homozygous
B
1

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.

Berwick answered 19/3, 2010 at 17:12 Comment(5)
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.Homozygous
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.Berwick
Here's a video tutorial explaining it - 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!Berwick
video is no longer available :(Wallacewallach
Is there an easy way to programmatically add a Rect to a UIComponent? UIComponents don't have addElement().Coal
H
2

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

Homozygous answered 19/3, 2010 at 17:8 Comment(0)
B
1

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.

Berwick answered 19/3, 2010 at 17:12 Comment(5)
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.Homozygous
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.Berwick
Here's a video tutorial explaining it - 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!Berwick
video is no longer available :(Wallacewallach
Is there an easy way to programmatically add a Rect to a UIComponent? UIComponents don't have addElement().Coal
H
0

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?

Hesperidium answered 19/3, 2010 at 16:57 Comment(0)
O
0

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

Overhead answered 19/3, 2010 at 16:58 Comment(0)
S
0

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

Significant answered 12/5, 2010 at 0:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.