0

I have a Flex ActionScript Application, I need to draw a simple rectangle in stage. I am using firstapp.mxml and another Class called Book.as;

here is the complete code which i have done.

firstapp.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute">

    <mx:Script>
        <![CDATA[
            import com.books.Book;
            import flash.display.*;

            var n:Book = new Book;
            //n.var1 = "Another String";
            addChild(n);
        ]]>
    </mx:Script>

</mx:Application>

Book.as

package com.books
{
    import flash.display.*;

    public class Book extends Sprite
    {
        public var var1:String = "Test Var";
        public var var2:Number = 1000;

        public function Book()
        {

            var b = new Sprite;
            b.graphics.beginFill(0xFF0000, 1);
            b.graphics.drawRect(0, 0, 500, 200);
            b.graphics.endFill();
            addChild(b);
        }

    }
}

I'm new in Flex, So please help me to fix this. I want to show the rectangle.

3 Answers 3

1

Since you're trying to add this to a flex component, you probably need to wrap Book in a UIComponent instance:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute">

    <mx:Script>
        <![CDATA[
            import com.books.Book;
            import flash.display.*;

            var n:UIComponent = new UIComponent;
            n.addChild(new Book);
            addChild(n);
        ]]>
    </mx:Script>

</mx:Application>

Another way of doing this would be to instead make Book inherit UIComponent, like so:

package com.books
{
    import flash.display.*;

    public class Book extends UIComponent
    {
        public var var1:String = "Test Var";
        public var var2:Number = 1000;

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            graphics.beginFill(0xff0000, 1);
            graphics.drawRect(0, 0, 500, 200);
            graphics.endfill();
        }
    }
}

Then you can add Book directly to your application, like so:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:books="com.books.*"
    layout="absolute">

    <books:Book />

</mx:Application>

Additionally, I suggest you read up on the Flex component architecture. There's some pretty good documentation from Adobe on the subject, but you should be aware that the information is specific to Flex 3 (I noticed you're using Flex 3, hence the link). Even while a lot of the information may still be applicable to Flex 4 (the component lifecycle for instance) there are differences, especially in terms of skinning.

Sign up to request clarification or add additional context in comments.

Comments

1

USE var n:Book = new Book(); instead of var n:Book = new Book;

4 Comments

Actually, it's valid ActionScript 3 syntax to omit the parenthesis.
@macke... Hmmm have you tested it ?
Yes, in fact I often use it to make code more readable. For instance, 'addChild(new Book)' reads better than 'addChild(new Book())'. I think that this only applies to constructors in conjunction with the 'new' keyword, however, not regular methods.
I think you may have problems with this, if it works then you're lucky but this is not the "normal" way of calling a constructor. What happens if the constructor needs params ;)
0

macke showed you good way. But if you want just to show something quickly, change

addChild(b);

to

rawChildren.addChild(b);

Edit: Some clarifications: application is UIComponent, therefore its addChild method needs UIComponent. You want to add Sprite. This can be done with rawChildren.addChild method from application. rawChildren is useful to get Sprites work with UIComponents, but you have to manage sprites yourself (sizes and layout).

1 Comment

It's probably useful to know what the difference between the two are, especially since rawChildren is an API that is anything but deprecated in Flex 4. This url might prove useful: billdwhite.com/wordpress/?p=296

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.