Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Code executing without button press (Beginner)


thisisforreal
 Share

Recommended Posts

Greetings people smarter than myself.  This is my first phaser self-guided exercise. I'm also very new to object oriented programming - and that may be the issue at hand.

 

I have a single gamestate, "playgame", which has a placeTiles() function that I don't want to execute until a user clicks a "Begin" button.  However, my code is not working as intended because the placeTiles() function is executing immediately upon loading.

 

I think there is a fundamental concept that I'm missing.  As you can see, I'm using preload, create, then some of my own -  placetiles, showtile.  Is it SOP for all of the functions inside playGame.prototype to run on initialization? If I don't want something running at that time, how do I prevent it or where should I move that block of code?  Wouldn't it need to go within playGame somewhere?

playGame.prototype = {	myMoney: 	null,	theEmpire: 	[],		preload: function(){		game.stage.setBackgroundColor(0xEBEBFF);  		game.load.spritesheet("gadgets", "gadgets.png", tileSize, tileSize);	},		create: function(){                   		// I've removed some code here where I create my background and color scheme		// Create the Begin Button which should place the other tiles.				var beginButton = game.add.button(  20,50,"gadgets",this.placeTiles(),this);		beginButton.frame = 10;				// There's a bunch more code here I've removed for this forum post */			},		placeTiles: function(){         		for(var i=0; i < numCols; i++){			var serialButton = game.add.button(     250 * (i+1)												,   game.height/5 + (i*tileSpacing)												,   "gadgets"												,   this.showTile												,   this);			serialButton.frame = i+1;			serialButton.value = this.theEmpire[i];		}//i++			},		showTile: function(target){		//More functions, etc...	  },};    game.state.add("PlayGame", playGame);     game.state.start("PlayGame"); };

Any explanation or guidance would be most appreciated.  Thank you for your time!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...