3

Maybe it is a bad habit which I still have since Flash Professional, but I'm trying to add a simple Sprite or/and Bitmap I just created to an empty Application and it's not happening.

<?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="955" minHeight="600" 
               creationComplete="application1_creationCompleteHandler(event)">

    <fx:Script>
        <![CDATA[
            import mx.core.FlexGlobals;
            import mx.core.UIComponent;
            import mx.events.FlexEvent; 


            protected function application1_creationCompleteHandler( event : FlexEvent ) : void
            {
                var s:Sprite = new Sprite();
                s.graphics.beginFill( 0xFF0000, 0.5 );
                s.graphics.drawRect( 0, 0, 100, 100 );
                s.graphics.endFill();

                this.addChild( s );
            }

        ]]>
    </fx:Script>
</s:Application>

Could someone tell me what I am doing wrong and help me correct it?

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
Yordan Yanakiev
  • 2,546
  • 3
  • 39
  • 88

2 Answers2

4

Try putting your sprite in UIComponent or SpriteVisualElement. This works for me:

<?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="955" minHeight="600"
               creationComplete="test()">

    <fx:Script>
        <![CDATA[
            import spark.core.SpriteVisualElement;

            private function test():void
            {
                var sprCont:SpriteVisualElement = new SpriteVisualElement();
                sprCont.horizontalCenter = 0;
                sprCont.verticalCenter = 0;

                var spr:Sprite = new Sprite();
                spr.graphics.beginFill(0xFF0000);
                spr.graphics.drawCircle(0,0, 100);
                spr.graphics.endFill();

                sprCont.addChild(spr);
                addElement(sprCont);
            }
        ]]>
    </fx:Script>
</s:Application>

For some reasons the 's:Application' tags are not shown, but they are there. HTH, FTQuest

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
FTQuest
  • 546
  • 3
  • 3
  • pfeewww ... i were forgot about "the new components with Flex 4.5 ... this done the job. Thank you. BTW- the application tags is not show, because you need to mark your whole source code as as code from the buttons above the text editor while you post answers. I edited it for you :) – Yordan Yanakiev Jun 27 '11 at 17:32
1

There are a few things to consider. You don't specify a height or width for your sprite. Won't it default to a height and width of 0; effectively rendering it invisible?

The second thing to note is that I strongly recommend you make use of the Flex Component Lifecycle methods for creating children. Instead of using a creationComplete handler, override createChildren() to create the sprite and add it. It would be common to use updateDisplayList() for the drawing, sizing, and positioning of the component.

The second thing to note is that Application is actually a sprite. Depending on what you're trying to do you could do the drawing directly on the application:

this.graphics.beginFill( 0xFF0000, 0.5 );
this.graphics.drawRect( 0, 0, 100, 100 );
this.graphics.endFill();

I perceive it will be more flexible to use a sprite than to draw directly on the Application; but it depends what you're trying to accomplish.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • I am trying to find a way with thoose things in order to create some kind of "background" element for MenuBar items. the idea overall is to having a Bitmap or Sprite in each MenuBar item, which to be used as a background. – Yordan Yanakiev Jun 27 '11 at 13:58
  • I that's your intent, would using the Background skin style work for you? http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/MenuBar.html#styleSummary – JeffryHouser Jun 27 '11 at 14:06
  • It is different for each Menu Item ? I tought it's global for all of the items... – Yordan Yanakiev Jun 27 '11 at 14:08
  • 1
    It depends how you set it up. I is true that a style can be set globally for a component in CSS. A style can also be set individually on the menu bar itself, which would override the global style setting. There is also a way to create a "named" style in CSS and then set the styleName property to that named style. – JeffryHouser Jun 27 '11 at 14:26
  • I see the idea now. The bad part is that the background will be a dinamic content actually, which i start to suppose that just a style wont do much. I really wish to add a simple sprite or bitmap to the Menu Item. – Yordan Yanakiev Jun 27 '11 at 14:58
  • The style takes a skin class as the parameter, and in the Spark theme that is just an MXML file (or ActionScript file). I'm not sure what you mean by dynamic in this case and why using that style will not work for you. – JeffryHouser Jun 27 '11 at 15:41
  • because - the idea is the background color for each element to change driven from a timer. – Yordan Yanakiev Jun 27 '11 at 17:26
  • I'm not understanding why you're having problems implementing that; but it appears you have solved your issue. – JeffryHouser Jun 27 '11 at 17:40
  • I really miss the part with SpriteVisualElement ... without it - the sprite just doesnt appear to the stage ... :| I went really crazy. I hope that Adobe will get fully rid of everything MX in Flex 5... Thank you once again @www.Flextras.com you also bring me to some points which i were missed with this issue. – Yordan Yanakiev Jun 27 '11 at 19:31
  • 1
    I have no doubt that MX will be here to stay strictly for backward compatibility reasons. But, we'll see how it goes long term. ;) I do expect the component set to be deprecated at some point. I'd guess Spark has component parity in Flex 5; and MX is deprecated in Flex 6; and removed in either Flex 7 or 8. So, 4-8 years based on my speculation. – JeffryHouser Jun 27 '11 at 19:56