views:

63

answers:

3
public class Greeter extends MovieClip
{

    public function Greeter()
    {
        addEventListener(Event.ADDED_TO_STAGE, go);
    }

    private function go(evt:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, go);
        var _root:MovieClip = parent.parent as MovieClip;

        var sp:Sprite = new Sprite();

        // Desenhando com um objeto graphics
        var g:Graphics = sp.graphics;
        g.beginFill(0xFF0000, 1);
        g.drawRect(10, 10, 300, 200);
        g.endFill();

        _root.addChild(sp);
    }
}

mxml file:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="1024" minHeight="768"
               creationComplete="initApp()">

        <fx:Script>
            <![CDATA[
                public function initApp():void
                {
                    var greeter:Greeter = new Greeter();
                }
            ]]>
        </fx:Script>

</s:Application>

--answer:

Add to the stage using addElement and extends spark.core.SpriteVisualElement.

A: 

You are only calling the constructor.

Inside the constructor you add an event listener.

Anywhere in you code you are drawing the rectangle.

Daniel Moura
(the negative was not mine) I'm drawing the rectangle on the go() method, that is fired by the ADDED_TO_STAGE event.. ;)
Tom Brito
Sorry I clicked the negative by mistake.
__dominic
now mine negative, this should be a comment, not an answer.. ;)
Tom Brito
+1  A: 

You need to add greeter to the stage

public function initApp():void
{
    var greeter = new Greeter();
    addChild( greeter );
}

When calling addChild( greeter ) it will trigger the event listener you added in the Greeter constructor and call the go method in which you draw your rectangle. Note that you don't need to do: _root.addChild( sp ); Since greeter is added to the stage in the initApp method, you can just add sp to greeter, by doing addChild( sp ) in the go method.

__dominic
Thanks! But now I got: "Error: addChild() is not available in this class. Instead, use addElement() or modify the skin, if you have one."; Using addElement gives the compiler error: "Implicit coercion of a value of type Greeter to an unrelated type mx.core:IVisualElement."
Tom Brito
The Greeter class needs to implement IVisualElement
__dominic
@__dominic when adding "implements IVisualElement" there is an error with a huge list of methods that needs to be implemented.. isn't there something I can just extend?
Tom Brito
I guess if all you want to do is add a display object to the stage maybe you should consider creating an AS3 project and not a Flex project. Anyway, what you can do is: Greeter extends SpriteVisualElement, and in the initApp method, use addElement() and not addChild
__dominic
Looks like this is what I was looking for... let me test..
Tom Brito
+1  A: 

Why are you doing this?

var _root:MovieClip = parent.parent as MovieClip;
...
_root.addChild(sp);

This style of display list management is reminiscent of ActionScript 2's leniency with encapsulation and polymorphism.

Your Greeter object should not be attempting to access items above it on the display list in this fashion. Greeter ought to be adding this rectangle as a child of itself, and there is no reason to use MovieClip objects in this context, Sprites are more appropriate.

To answer your question and fix the resulting answer about addChild, the use of the Spark Application instead of mx:Application is the reason why addChild is not a valid function. Spark application instances must contain a new breed of display object, as above, either an extension of SpriteVisualElement or an object that implements the functions defined in IVisualElement.

If you are not leveraging Spark's layout and skinning functionality, simply change to using Flex 3/Halo/mx Application tag, and you will be able to addChild() as expected with a standard object extending Sprite instead of SpriteVisualElement.

Tegeril
I myself am not doing that, I just got an sample from the web.. there's no much using ActionScript 3 for game development.. Now, I'll try extend the SpriteVisualElement and I back here..
Tom Brito