Using Flash Component SWC File In Flex

- 1 answer

I am accessing custom UIComponent via SWC file from Flex 3. This component works OK in Flash CS3, but using it from Flex gives A weird error in draw().

I have added swc component inside Sprite (with addchild) and its in LIB path.

TypeError: Error #1010: A term is undefined and has no properties.

at com.xxxx.highscores::HighScores/draw()

at fl.core::UIComponent/callLaterDispatcher()

Here is the draw() function of this UI Component:

override protected function draw():void { isInitializing = false;

     page.Text.x = width / 2;
     page.Text.y = height / 2;





With only that code, it must be either page, or page.Text, is null.

Going by the names, I would guess page is a Flash library object you create with AS? If so, I would guess a previous error is firing before it is created and being swollowed by the player (can happen if the debugger has not attached yet, or problems with loading shared librarys). 'stage' not being set for new display object until it's added to the display list is common.

EDIT: It's a bug in the component: draw() always uses the highScoresModuleText property on page: which is only set when the page is a HighScoresTextPage, and not any of the other pages, eg: HighScoresTablePage, which showHighsSores() sets it to. This works in Flash presumably because the object is on the stage, or at least gets created before showHighScores() is called, so draw() gets called first, and since the component does not invalidate, is not called after.

The correct method in this case is to have show*() just set some properties, then invalidate() to have draw() figure it out later, but a quick fix is to just add 'if (page.highScoresModuleText)' around the offending lines in draw(). An even quicker fix is to create and addChild() the component early (like startup), and call showHighScores() much later.

This works for me:

    import flash.display.Sprite;
    import com.novelgames.flashgames.highscores.HighScores;

    public class As3_scratch extends Sprite
        private var highscore : HighScores;

        public function As3_scratch()
            highscore = new HighScores();
            stage.addEventListener(MouseEvent.CLICK, onClick);

        private function onClick(event : MouseEvent) : void