views:

51

answers:

4

Ok I'm trying to draw a box with actionscript 3 and flex 3.5. First a few things:

1) I am not using any IDE at all, just notepad and flex 2) I am not using any mxml file at all (yes this is possible with as3 apparently) 3) I am not using inheritance ie I'm not using an extends on my classes, I'm trying to get this particular bit of code to work only through composition

Anyway this is the code itself (the file name is MAIN.as):

package 
{
    import flash.display.*;     
    import mx.core.Application;     
    import flash.events.*;      
    import mx.collections.*;
    import flash.geom.*;        
    import mx.controls.*;
    import flash.text.*;        
    import mx.events.*;
    import mx.styles.*;

    public class MAIN
    {
        public var APPLICATION:Application = Application(Application.application);
        public function MAIN()
        {
            APPLICATION.layout = 'absolute';
            var BOX:Sprite = new Sprite();
            BOX.graphics.beginFill(0xFF0000,1);
            BOX.graphics.drawRect(0,0,400,400);
            BOX.graphics.endFill();
            APPLICATION.addChild(BOX);
        }
    }
}

It compiles just fine but isn't displaying anything. I should see a red box but right now all I'm seeing is a blank screen. I've gotten this to work by using class MAIN extends Sprite and then getting rid of the box variable all together but again I want to avoid using the extends command. Sorry that the imports got kinda smooshed in the post, they aren't really relevant to the problem though so it doesn't matter all that much.

A: 

Flex components (such as Application, List, Canvas, etc) can only add other Flex Components as children when using addChild. If you want to add a raw AS3 primitive like Shape, Sprite, Movieclip, you have to do [FLEX COMPONENT HERE].rawChildren.addChild(BOX); or in your case APPLICATION.rawChildren.addChild(BOX);

Newtang
A: 

I tried changing it and it compiled fined but still nothing is showing up. Do I just have to change that one line?

1101
A: 

I think you should be extending Sprite if that is your main class.

Second, I'm not an expert on Flex, but I think you can't really use most of Flex's classes if you haven't properly initialized the framework. I've always seen this done using mxml. Probably there's a way to do this using Actionscript only, but I don't see the point. If you want to use Application and other Flex stuff, make your entry point an mxml file. Otherwise, just use pure Actionscript.

PS: Just read that you had already figured out your code works extending Sprite. But whatever your preferences about inheritance or composition are, for your box to be visible, it has to be attached to the display list, at some point. So, you have a Sprite and have drawn to it. But you still have to add it to the display list to see it. To add a display object to the display list, you need a reference to some display object container. If you extend Sprite, your class will be 1) a container and 2) will be attached to the display list. Your main class should be at least, a Sprite. You don't have much choice here, I think.

Juan Pablo Califano
A: 

The Document Class is your entry point for ActionScript to the stage. An instance of your Document Class object is present and representable as the "root" property of any object also on the display list. The root property of the DisplayObject class must return a DisplayObject, defined as:

For a display object in a loaded SWF file, the root property is the top-most display object in the portion of the display list's tree structure represented by that SWF file.

Therefore, your document class -must- be a DisplayObject in order for anything to display on the stage, regardless of any desires to use composition.

Regarding Flex components, there is no need for any reference to mx.core.Application or any other Halo components, if your primary AS file extends from a DisplayObject (Sprite, primarily, MovieClip has no use unless you are involving an object from Flash CS* Professional that contains a timeline that you need to reference in frames), you can still create the BOX:Sprite and add it to the document class's display list

this.addChild(BOX);

And everything will work.

Tegeril