var characters_:Array<Character>;
package ;
class Character
{
public var name:String;
public function new(_name:String)
{
trace("I am a new character");
name = _name;
trace("My name is " + name);
}
}
//Test character creation
trace("Creating a new character");
var test:Character = new Character("Tom"); //Works correctly
trace("Creating another new character");
characters_.push(new Character("Bill") ); //Prints out correctly, but gives an error
trace("A new character was created successfully");//Does not print
> var characters_:Array<Character>;
Should be
> var characters_:Array<Character> = [];
Basically when you declare a variable in Haxe, you need to initialize it or else it will be null.
If that's not the issue maybe post a more complete code sample :)
Good luck!
Jason
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups "Haxe" group.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
I bet you forgot to create characters array :)
package ;
import flash.display.Sprite;
import flash.events.Event;
import flash.Lib;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
//My own classes
import Character;
/**
* ...
* @author Mandemon
*/
enum STATUS { MENU; INGAME; PAUSED; }
class Main extends Sprite
{
var inited:Bool;
var state_:STATUS;
var characters_:Array<Character> = []; //This one gives the error, Variable initialization must be a constant value
/* ENTRY POINT */
function resize(e)
{
if (!inited) init();
// else (resize or orientation change)
}
function init()
{
if (inited) return;
inited = true;
// (your code here)
state_ = MENU;
trace(state_);
//Test character creation
trace("Creating a new character");
var test:Character = new Character("Tom");
trace("Creating another new character");
characters_.push(new Character("Bill") );
trace("A new character was created successfully");
//var stats:Array<Int> = characters_[0].getStats();
// Stage:
// stage.stageWidth x stage.stageHeight @ stage.dpiScale
// Assets:
// nme.Assets.getBitmapData("img/assetname.jpg");
}
/* SETUP */
public function new()
{
super();
addEventListener(Event.ADDED_TO_STAGE, added);
}
function added(e)
{
removeEventListener(Event.ADDED_TO_STAGE, added);
stage.addEventListener(Event.RESIZE, resize);
#if ios
haxe.Timer.delay(init, 100); // iOS 6
#else
init();
#end
}
public static function main()
{
// static entry point
Lib.current.stage.align = flash.display.StageAlign.TOP_LEFT;
Lib.current.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
Lib.current.addChild(new Main());
}
}
package ;
import flash.display.Sprite;
import flash.events.Event;
import flash.Lib;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
//My own classes
import Character;
/**
* ...
* @author Mandemon
*/
enum STATUS { MENU; INGAME; PAUSED; }
class Main extends Sprite
{
var inited:Bool;
var state_:STATUS;
var characters_:Array<Character>; //Don't initialize here, Haxe can't initialize class instance variables with anything other than int/string/constants.
/* ENTRY POINT */
function resize(e)
{
if (!inited) init();
// else (resize or orientation change)
}
function init()
{
if (inited) return;
inited = true;
// (initialize the characters array)
characters_ = [];