Access of undefined property/method [Solved]


(Lozza JP) #1

Hi I am trying to get around this error, if I put the text code in the constructor it pops up fine in the game. If I try to make this method I get the error as seen in the picture.

Any help/direction greatly appreciated!

I can’t upload as a new user here is a dropbox share link for the image.


(JP Mortiboys) #2

It’s because you’ve declared setDoorText as static. As a static method it doesn’t have access to any member variables (or member methods) - because it’s not associated to the instance of the class, but to the class itself.

TLDR: Don’t use static in this situation.


(Lozza JP) #3

So to call this method, I would have to use an instance of GameWorld (the class this is from).

Trying to make walking next to a door show some text, but seem to only be able to get GameWorld to show text :confused:


(JP Mortiboys) #4

No, you need to remove the static declaration on the definition, like so:

public function setDoorText():void {
  // ...
}

This function should not be static, it makes no sense for it to be.


(Lozza JP) #5

Then I get an error, call to possibly undefined method setDoorText.

setDoorText is in my GameWorld class, which the instance is FP.world…? I think?

Started my main method like the tutes, FP.world = new GameWorld(…)


(JP Mortiboys) #6

Ah, I see. In any case static functions won’t be able to help here, they’re not applicable.

What you need to do is to cast the reference to the world from the base class World to the derived class GameWorld, and then access the method. You can do this in three ways - either instantly:

(world as GameWorld).setDoorText(...)

Or by declaring a local variable:

var gameWorld:GameWorld = world as GameWorld;
gameWorld.setDoorText(...)

Or by having that variable cached at entity level (or wherever you’re calling it from).

One point is that FP.world should ONLY be used to change the active world, otherwise odd unexpected behaviours can crop up. To get a reference to the current world, from an entity just use world (except in the constructor… but that’s another story).


(Lozza JP) #7

wow got it working thanks :smiley:

at first I tried to make it more of a property at the top but it caused a crash. Will keep tinkering thanks NotARaptor

p.s. how can i mark this solved?


(Jacob Albano) #8

Edit the first post in the thread and change the title.