Be a Supporter!

Flash says its undefined, but it is

  • 319 Views
  • 7 Replies
New Topic Respond to this Topic
Version2
Version2
  • Member since: Sep. 24, 2003
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Flash says its undefined, but it is 2012-02-03 06:08:55 Reply

I started working again on one of my older projects and ran into a error that shouldn't be happening. I'm getting an undefined property (error 1120) on something that has been clearly defined in the first few lines of code.

The variable "scrnHand" says it's undefined when I try to run the program. However you can clearly see that I make it into a new ScreenHandler almost first thing. Here is the code:

public var scrnHand:ScreenHandler;
		public var scrnHolder:Sprite
		
		public var startScreen:String
		
		public function Main() 
		{
			scrnHolder = new Sprite();
			addChild(scrnHolder);
			
			scrnHand = new ScreenHandler(scrnHolder); //defining scrnHand here?
			
			compileScreens();
			firstScreen();
			
			addEventListener(Event.ENTER_FRAME, updater);
		}

But where I get the error is here in this section:

public static function getScrnHand():ScreenHandler
		{
			return scrnHand; //error 1120, scrnHand is undefined?
		}

Even if I change it so nothing calls getScrnHand() it still throws the error first thing, I don't even get my traces in the output window, just error 1120. Does anyone have an idea why it's saying scrnHand is undefined on that line?

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Flash says its undefined, but it is 2012-02-03 08:03:18 Reply

Try to understand what the keyword "static" means.
Think about why a static method cannot know a property that is not static.
As far as your code goes, making the method static makes no sense.

Also keep properties as private as possible.

Version2
Version2
  • Member since: Sep. 24, 2003
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Flash says its undefined, but it is 2012-02-03 08:39:09 Reply

Well, the reason it was static was because when I called the function from a different class I would get an error 1061: Call to a possibly undefined method getScrnHand through a reference with static type Class.

that error gets thrown when this runs and getScrnHand() isn't static:

private function endScreen(e:Event):void 
		{
			Main.getScrnHand().createScreen("GameScreen");
			Main.getScrnHand().showNextScreen();
		}
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Flash says its undefined, but it is 2012-02-03 09:02:05 Reply

You have the illusion that the keyword "static" is made to reference everything from everywhere.

Now you reap what you sow.

Stop abusing "static" and start proper oo coding.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Flash says its undefined, but it is 2012-02-03 09:41:49 Reply

At 2/3/12 06:08 AM, Version2 wrote: public static function getScrnHand():ScreenHandler
{
return scrnHand; //error 1120, scrnHand is undefined?
}

You are attempting to access "scrnHand", which is defined as a non-static object, through a static function.
Static functions only have access to static members.

milchreis is right. You don't know what static means and you need to stop using it.

Version2
Version2
  • Member since: Sep. 24, 2003
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Flash says its undefined, but it is 2012-02-03 10:45:31 Reply

At 2/3/12 09:41 AM, Diki wrote:
milchreis is right. You don't know what static means and you need to stop using it.

I said in my second post that I stopped using static in that function. I'm still getting errors when I try to use that function, as I explained in my second post. Static isn't being used anywhere in my code anymore, however I still can't access my screen handler from any class outside of Main for some reason.

Version2
Version2
  • Member since: Sep. 24, 2003
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to Flash says its undefined, but it is 2012-02-03 10:47:20 Reply

At 2/3/12 10:45 AM, Version2 wrote:
I said in my second post that I stopped using static in that function.

err... rather I subtly implied it. But no, it's not being used anymore.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Flash says its undefined, but it is 2012-02-03 11:45:04 Reply

In the code in your second post you are still trying to use a static function.
If you attempt to access a member variable or function through a class reference then you are always accessing a static member.

The problem is you referencing functions through Main.