[F8] Class scope issues with Delegate
Alright so I'm working on the inventory system for my game and I'm running across more scope issues with key listeners.
In my Game class constructor, I have this line:
Code:
keyListener.onKeyDown = Delegate.create(this, listenForKeys);
Then later in my class file I have the listenForKeys function:
Code:
private function listenForKeys():Void
{
if(Key.getCode() == Key.SPACE)
{
// lots of code here, but specifically a
// call to the variable called "inventoryList",
//which is an array of strings
}
if(Key.getCode() == Key.LEFT)
{
// my code
}
if(Key.getCode() == Key.RIGHT)
{
// my code
}
if(Key.getCode() == Key.UP)
{
// my code
}
if(Key.getCode() == Key.DOWN)
{
// my code
}
Key.addListener(keyListener);
}
Now shouldn't the Delegate line I added to the constructor force the listenForKeys method to use the main scope of the class? I have declared the inventoryList array before the constructor and assigned it a value (inventoryList = ["--", "--", "--", "--"]) in a regular function called loadUI() that is called in the constructor as well.
In my listenForKeys method, I have a line of code that does a .push to the inventoryList array to add the new item picked up by the hero to the inventory. Instead of adding it, the item I attempt to push ends up just "replacing" the first element and then "deleting" the rest of the elements. It's obviously not actually replacing or deleting, but instead creating it's own inventoryList and pushing values to that blank array.
What am I supposed to do to correct this scope issue? Did I use Delegate incorrectly?