View Full Version : class members not being set

11-17-2009, 12:46 AM
I have a project I used to compile fine with flash, now I'm trying to compile it with flex. I have a fla full of assets, and some as files. Some of the as files are linked to assets, such as StartScreen.as which extends Sprite and is linked to a library item I made in the gui.

Basically, I got all this working. I added the necessary source paths to flex, and compiled a swc with flash, and added the path to the swc to flex. I get no compiler errors. However, I get runtime errors, because for example startScreen.playButton is null. My StartScreen class is like this:

import flash.display.Sprite;
import flash.display.SimpleButton
import flash.events.MouseEvent;

public class StartScreen extends Sprite
public var playButton:SimpleButton;

protected var playCallback:Function;

public function StartScreen(playCallback:Function)
this.playCallback = playCallback;
playButton.addEventListener(MouseEvent.CLICK, playClickListener, false, 0, true);

protected function playClickListener(event:MouseEvent):void

And it's linked in the fla to a library item. However, after I compile in flex and try to run, I get a null reference error on the line I try to add the event listener, because playButton is null. How can I fix this?

When I compile with flash, the button I added in the gui and named "playButton" is automatically assigned to the member I called "playButton" in the as file. Is this not the case with flex?

Barna Biro
11-17-2009, 04:11 PM
Inevitably, Flex Builder will be filled with error is you are only using it as an editor and you have all your objects in the .fla. Flex Builder can't guess that the objects he can't find in the class might exists in a .fla somewhere. Shortly put: you will most likely not have code that can be compiled and even functional in Flex Builder if you are only using it as an editor.

Either you think of a different way to build your application, or you simply learn to live with it. You can use this.getChildByName("playButton") and store the returned reference ( if any ). If a reference is returned, you can add a listener to it, else, it will throw errors.

Try surfing the internet because there are surely more articles on this issue. Good luck.

11-17-2009, 06:07 PM
Thanks for your reply. Flex is looking in the swc I compiled from the fla, not the fla itself. I did try getChildByName, and it returns null. In the first line of my constructor, I put

trace("StartScreen constructor");
playButton = SimpleButton(getChildByName("playButton"));

and I do see the trace appear, but playButton is still null when I try to add the listener. It's as if the swc I compiled with flash does not contain the instance names I entered in the gui. It finds the as file for StartScreen and links it to the correct library item (so I see the trace statement), but it doesn't name the child instances correctly. This seems like a problem with flash, or maybe just something I haven't set up correctly when I compiled the swc, and not so much a fundamental incompatibility between flex and flash code.

11-17-2009, 06:45 PM
I just added trace("numChildren: " + numChildren) to the constructor, and got 0. It also comes back 0 after the constructor. This makes no sense at all - I did add children in the flash gui.

In general, what is a good way to access children of a library item I made in the flash gui? Say I make a library item called StartScreen in flash, and in the gui I add a child which is a SimpleButton. Now say I want to use flex to compile my final swf, and in the code I need to refer to the SimpleButton. What is the "right" way of getting a reference to it? If I can't use the instance name I give it in the gui, all I can think to do is iterate through all the children of StartScreen until I find a SimpleButton, which won't even work because there are more than 1 SimpleButton (and apparently, none of the children are actually getting attached anyway.) What is the right way to do this?