I can't access my array in update()

29 views
Skip to first unread message

Gold_Ninja

unread,
Nov 3, 2014, 10:52:16 AM11/3/14
to haxef...@googlegroups.com
Hi, I can't read from or write to my array in the update() function.
Here's my code:

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.math.FlxMath;
import haxe.xml.Fast;
import sys.io.File;

class MenuState extends FlxState
{    private var someText:FlxText;

   
private var myArray:Array<String>;
   
   
override public function create():Void
   
{
        someText
= new FlxText(20, 20, 300, "");
        add
(someText);
       
       
var myArray = new Array<String>();
       
        myArray
[0] = "File 1";
        myArray
[1] = "File 2";
        myArray
[2] = "File 3";
       
       
super.create();
   
}
   
   
override public function update(elapsed:Float):Void
   
{
        someText
.text = myArray[0];
       
       
super.update(elapsed);
       
   
}
}

I get the following error: Custom([File_write,stderr])

Any help would be greatly appreciated.

SruloArt

unread,
Nov 3, 2014, 11:18:04 AM11/3/14
to haxef...@googlegroups.com
You have a spare "var" here: "var myArray = new Array<String>();" just remove it.

Gold_Ninja

unread,
Nov 3, 2014, 12:54:45 PM11/3/14
to haxef...@googlegroups.com
That didn't help, I'm afraid. If I comment out the line you suggested it fails in create()

Thanks for trying.

Gama11

unread,
Nov 3, 2014, 1:21:07 PM11/3/14
to haxef...@googlegroups.com
You shouldn't remove the entire line, just the var keyword. Having that there creates myArray as a local variable that only exists in the context of create(). The myArray variable at class level remains uninitialized, which is why you're getting a null error.

Gold_Ninja

unread,
Nov 3, 2014, 1:29:00 PM11/3/14
to
Thanks for clarifying Gama11, I misunderstood SruloArt's answer and just commented the whole thing out.  Works perfectly now.

Thanks both of you!
Reply all
Reply to author
Forward
0 new messages