Student
Student

Reputation: 28375

Why this rectangle is not drawn?

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.

Upvotes: 1

Views: 497

Answers (3)

Aaron
Aaron

Reputation: 4614

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.

Upvotes: 1

dotminic
dotminic

Reputation: 1145

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.

Upvotes: 2

Daniel Moura
Daniel Moura

Reputation: 7956

You are only calling the constructor.

Inside the constructor you add an event listener.

Anywhere in you code you are drawing the rectangle.

Upvotes: -1

Related Questions