views:

3150

answers:

2

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;

     drawBackground();

}

+3  A: 

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:

package
{
    import flash.display.Sprite;
    import com.novelgames.flashgames.highscores.HighScores;
    import flash.events.MouseEvent;

    public class As3_scratch extends Sprite
    {
     private var highscore : HighScores;

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

     private function onClick(event : MouseEvent) : void
     {
      highscore.showEnterHighScore(50);
     }
    }
}
Simon Buchan
Thanks, I got rid of error now, but still nothing appears on screen when I give these commands in flex:highscore.showEnterHighScore(_score);highscore.drawNow();I guess this is another bug.
Tom
What happens if you just call showEnterHighScore? the show*() methods all seem to update the display immediately, and don't seem to work with draw() (so it's kind of broken)
Simon Buchan
:( nothing happens, nothing appears on screen,but this function call works instead:highscore.showHighScores();highscore.drawNow();
Tom
ps. drawnow is required, otherwise it remains blank
Tom
Odd, it did for me for showHighScore(); I will have to check out showEnterHighScore() when I get back to work (Hah!) on monday.
Simon Buchan
hi Simon, any news on this one yet?
Tom
yes it works with mouseclick, but this does not? { highscore = new HighScores(); addChild(highscore); highscore.showEnterHighScore(50); }note I added add 'if (page.highScoresModuleText)' around the offending lines in draw()
Tom
it appears this weird delay there (mouse click) is required to make it work.. I will try to call drawnow() or something before .show()
Tom
Thanks a lot! Got it work finally with a call to DRAWNOW() function before calling .show()
Tom
Yes, I was trying to show that it needs that delay - Sorry that wasn't clear!
Simon Buchan
A: 

Here is the download link to this (free) flash component with source code http://www.novelgames.com/otherdownloads/highscores/

this is how I call it from Flex project:

  addChild(highscore);
  highscore.showHighScores();

--> crash occurs soon after this

Tom
BTW, the stackoverflowy way to add info is to edit the original post, otherwise the info can get lost, and you don't get listed on unanswered questions.
Simon Buchan