Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.18 / 5.00 3,534 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.80 / 5.00 4,200 ViewsI 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?
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.
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();
} 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.
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.
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.
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.
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.