I think someone else already explained that you can simply draw into a
UIComponent rather than into a Sprite or a Shape. All three of these
have a 'graphics' property which contains a reference to an instance of
the flash.display.Graphics class, which has methods like drawCircle().
But a UIComponent will "just work" with Flex containers like VBox while
Sprite and Shape require you to do more work.
 
Here's the Flex Way to create a Ball component (in AS, but you could
also do it in MXML) that extends UIComponent, and then use it in an MXML
app:
 
Ball.as:
----------
 
package
{
 
import flash.display.Graphics;
import mx.core.UIComponent;
 
public class Ball extends UIComponent
{
    public function Ball()
    {
        super();
    }
 
    override protected function updateDisplayList(unscaledWidth:Number,
 
unscaledHeight:Number):void
    {
        super.updateDisplayList(unscaledWidth, unscaledHeight);

        var g:Graphics = graphics;
        g.clear();
        g.beginFill(0x000000);
        g.drawEllipse(0, 0, unscaledWidth, unscaledHeight);
        g.endFill();
    }
}
 
}

BallExample.mxml:
---------------------------
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> " xmlns:my="*">
    <my:Ball width="100" height="100"/>
    <my:Ball width="200" height="200"/>
</mx:Application>
 
You can see that Flex has its own ways of doing certain things that you
won't find in a book only about ActionScript. For example, you should do
all of your drawing in an override of the updateDisplayList() method, in
order to work properly with Flex's LayoutManager.
 
Now you're a Flex component developer! Next you could learn how to make
the ball have some size even if you don't specify 'width' and 'height'
in the <mx:Ball> tag. Or add a style to determine the ball's color using
CSS. Or add an event handler to change the color when the user clicks on
it. Or let your user create new Ball instances at runtime by doing
something. Or use effects to move the balls around. Or whatever...
 
As for whether there are examples like this somewhere in our docs... I
sure hope so! But I can't say for sure as I'm on the development team,
not the documentation team, and I haven't personally looked through an
entire documentation set, which is huge.
 
Gordon Smith
Adobe Flex SDK Team

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of [EMAIL PROTECTED]
Sent: Monday, January 21, 2008 6:03 PM
To: [email protected]
Subject: Re: [flexcoders] Re: addchild





Gordon Smith wrote:
>
> > Why would you think that a book called "actionscript 3 animation" 
> uses only only
> > flash.* classes throughout and avoids the mx.* classes completely ?
> 
> It was just a guess, because the title didn't include the word "Flex".

> Are you saying that this book actually does use mx.* classes?

No it doesn't.You were correct, I just wondered how you would know.

Thanks for the explanations.I still don't understand why there's no 
example of drawing a shape in the flex documentation that doesn't use 
flash.* classes. I understand that the flash.* and sprite based examples

can be children in a flex application, but you seem to be saying that 
shapes can be created without those classes.

If not, then maybe I misunderstood when you said to start with the flex 
classes. Maybe you were saying, in effect not to start with shapes.

It that correct ?

> 
> > Doesn't Flex use actionscript 3?
> 
> Yes. As I said, you write AS3 to use both the flash.* classes (which 
> are actually implemented in C++) and the mx.* classes (which are 
> themselves implemented in AS3). And, as I said, the former are 
> low-level classes built into the Player while the latter are 
> high-level classes provided with the Flex framework which get linked 
> into your SWF.
> 
> > Doesn't it do animation?
> 
> Yes, it can "do" animation. In fact, Flex's mx.effects.* classes ARE 
> animation classes. But we didn't create Flex to make it easier to make

> balls move around the stage. The focus of Flex is not Flash-style 
> animations and games, although some developers use it to create just 
> that. Flex is really focused on building Rich Internet Applications, 
> and we provide the most common components that are necessary to do 
> that... things like ComboBox, DataGrid, DateChooser, etc.
> 
> Gordon Smith
> Adobe Flex SDK Team



 

Reply via email to