The Ice Mage
The Ice Mage

Reputation: 445

How to pass values to phaser (Dart port) State.init() function?

I have the beginnings of my game code structured like this

main() {
new Foo();
}

class Foo {

GameMap map;

Foo() {
  map = new GameMap(32, 32); //GameMap is a class of my own
  ... // do some map-related initialisation
  Game game = new Game(800, 600, AUTO, 'canvasDiv');
  State state, state2; 
  state = new FileWaitState(); //explained below
  game.state.add('wait', state);
  game.state.start('wait');
  state2 = new MapRenderState();
  game.state.add('maprender', state2);
}

So, the idea with FileWaitState is that an algorithm needs to run to analyse a text file submitted by the user/player and until that analysis is complete my second state, currently called MapRenderState must not start. To facilitate this FileWaitState has in its update() function a check to see if the file has been given, and if it has it starts the async. function call to analyse the file, which has a .then() chained onto it like

.then(_) {
  game.state.start('maprender');
}

Now, my problem is that because the instance of my FileProcessor which I must call the analysis function on is nested inside GameMap, I want a way to pass a reference to the map from Foo constructor into FileWaitState. (Sort of an aside: and even if somehow or for some reason I was to restructure code so that I wouldn't need a map reference here, if possible, I will have the same kind of problem for MapRenderState.) I have seen that Phaser's State has an init() function which is the first thing to be called on a newly started state, and it allows for a variable amount of args to be passed into it. What I was trying to figure out though is how am I supposed to specify what args are to be passed into it, or if I'm approaching this wrongly? Surely there is a way to send values in there, right? Otherwise what's the point of init() allowing any args? I don't mean declaring what it's supposed to take; obviously I just need to write my init implementation in whatever State class. I mean, going back to my code, if I say init is to take one arg of type GameMap then is there a way to say somewhere "when calling init for this State, pass in this GameMap [from Foo ctor] as the arg"

I apologise if there is something basic about the framework that I am missing here but even after looking through some of the original Phaser.js API docs (since the dart port docs aren't all complete yet) I haven't figured out what I could do to get this working. I guess there's also the fact that I'm a bit "under the weather" at the moment so I don't feel up for doing a lot of programming work right now but at the same time I have put some of this work off a bit too long so I would like to get this code roadblock out of the way so that I have something less demotivating to work on for this project.

TL;DR attempt

I have a State (FileWaitState) and want to pass a reference to a GameMap into its init() function because the State's update function will need to access GameMap data. My problem is that this code

map.fileProcessor.analyseTxtFile(ie.files[0]) in the update func gives the error The null object does not have a getter fileProcessor or so because obviously I haven't passed in a reference to the GameMap to make the map var inside the state not null. My question is how am I supposed to pass that reference in?

Upvotes: 1

Views: 1069

Answers (1)

Yiou
Yiou

Reputation: 790

I'm not exactly sure what you mean, but if you want to pass something to initialize a state, you can do that in the constructor of the state. For example your state constructor could be

MapRenderState(this.map){//do something about map}

Then you can do

game.state.add('render',new MapRenderState(map));
game.state.start('render');

If you want to load a map in a state and use the map in the same state, do the loading in preload() . I recommend using phaser's loading function. game.load.text(key,url) will load a text file.

Upvotes: 1

Related Questions