Jump to content

using Custom Phaser.Signal( pause unpause game)


isekream
 Share

Recommended Posts

I have on an extended Phaser.Game class the following 

let game = this;
game.onGameStart = Phaser.Signal();

function startGame(){
   game.onGameStart.dispatch();
}

in an extended Phaser.State class I have the following

create(){
let game = this.game;
game.onGameStart.add(this.start, this);
game.message.on('message', function(){ game.startGame(); } // message is a node server web socket remote trigger response
game.paused = true;
}
function start()
{
this.game.paused = false;
}

When game.startGame() gets called start its called but the game._paused and game._codePaused is still set to true.

 

 

WHY is this?

 

Link to comment
Share on other sites

I don't see where you're calling startGame? You didn't list "game._codePaused" in your code, so I'm not sure what sets that. But since your "start" method is the one that sets "game.paused = true;" then it makes sense that it wouldn't be set to true before that runs.

I'm feeling confused.

Link to comment
Share on other sites

7 minutes ago, drhayes said:

I don't see where you're calling startGame? You didn't list "game._codePaused" in your code, so I'm not sure what sets that. But since your "start" method is the one that sets "game.paused = true;" then it makes sense that it wouldn't be set to true before that runs.

I'm feeling confused.

Sorry if it doesn't seem clear

"startGame" is being called by an anonymous function created in the state as shown in the 2nd snippet. game._codePaused is apparently a private var in the Phaser.Game class when i outputted it to log.

 

This is driving me nuts as all documentation and research is showing is that to pause and unease a game is a simple matter of setting the variable...I'm at odds end as to why it isn't "unpausing"

 

Link to comment
Share on other sites

Did you stick a console log inside your start function to make sure it is called?

function start() {
  ...code...
}

That is a function inside somewhere (your snippet does not show where), unless you apply that to `this.start` it is not equal to `this.start` so it'll never get called and set the variable to false.

Even if it is being called I think you might be confusing which bits are asynchronous and which bits run synchronously, because `startGame` dispatches the message that changes the `paused` variable, where are you checking what it is set to?

Also, I'm not down with how Phaser signals work, is the dispatch synchronous? I expect it would be.

Link to comment
Share on other sites

15 minutes ago, mattstyles said:

Did you stick a console log inside your start function to make sure it is called?


function start() {
  ...code...
}

That is a function inside somewhere (your snippet does not show where), unless you apply that to `this.start` it is not equal to `this.start` so it'll never get called and set the variable to false.

Even if it is being called I think you might be confusing which bits are asynchronous and which bits run synchronously, because `startGame` dispatches the message that changes the `paused` variable, where are you checking what it is set to?

Also, I'm not down with how Phaser signals work, is the dispatch synchronous? I expect it would be.

class GameStatePlay extends Phaser.State {

	init()
	{
			this.game.paused = true;
			this.onEndGamePlayEvent = new Phaser.Signal();
			this.onStartGamePlayEvent = new Phaser.Signal();
			this.onStartGamePlayEvent.add(this.startGame, this);
	}

	create() {

			let game = this.game;
			let playerCollision = game.physics.p2.createCollisionGroup();
			let ballCollision = game.physics.p2.createCollisionGroup();
			game.physics.p2.updateBoundsCollisionGroup();

			let timeBoard = new TimeBoard(game);
			game.world.addChild(timeBoard);

			let timer = game.time.create(false);
			timer.lastDroppedBall = 0;
			timer.loop(1, timeBoard.updateTimeText, timeBoard, timer);
			timer.start();
			// load player
			let player = new Player(game);
			game.physics.p2.enable(player);
			game.world.add(player);
			player.body.clearShapes();
			player.body.mass = 200;
			player.body.damping = 1;
			player.body.kinematic = true;
			player.body.loadPolygon('playerPolygon','player');
			player.body.setCollisionGroup(playerCollision);
			player.body.collides(ballCollision, player.updateScore, player);
			player.body.allowRotation = false;
			player.body.fixedRotation = true;
			player.body.immovable = true;
			player.body.onBeginContact.add(player.headerBall, player);
			player.statistics = game.cache.getPhysicsData('playerPolygon', 'player');
			player.assignShapeNames();
			player.onUpdateScoreEvent.add(timeBoard.updateScoreText, timeBoard, 0, timer);
			// start two way
			game.client.subscribe('/faye/desktop/move/' + game.uniqid , function(e){
				player.move.execute(e);
			} );
			// load ball
			let ball = new Ball(game);
			ball.checkWorldBounds = true;
			game.physics.p2.enable(ball);
			game.world.add(ball);
			ball.body.mass = 0.43;
			ball.body.damping = 0.151;
			ball.body.setCircle(28);
			ball.body.allowRotation = true;
			ball.body.fixedRotation = false;
			ball.body.adjustCenterOfMass();
			ball.body.setCollisionGroup(ballCollision);
			ball.body.collides(playerCollision);
			ball.events.onOutOfBounds.add(this.hasDroppedBall, timer);
			ball.body.enabled = false;

			let st = this;
			game.client.subscribe('/faye/desktop/start/' + game.uniqid , function(){
				st.onStartGamePlayEvent.dispatch();
			});
	}

	startGame()
	{
			this.game.paused = false;
			this.game.enableStep();
			this.game.step();
			console.log(this.game.paused);
	}

	hasDroppedBall(time){
		let timer = this;
		if(timer.running && ((time - timer.lastDroppedBall) < 1000))
		{
				timer.stop(true);
				setTimeout(function(){
					timer.game.state.start('end');
				},1000);
		}

		timer.lastDroppedBall = time;
	}
}

 

I'm was doing some more modifications to see how game.paused works;

 

As you can see GameState is inherited State Class..

 

Everything happens in the create() 

"game.client.subscribe" is a pub/sub node messaging and when it receives a message it triggers to dispatch a phaser.signal that should "un pause" the game

Yes. it has been triggered and yes logs show game.paused as being false. 

 

I note if i game.paused = true in init() and then game.paused = false in the create() the game works as expected. My only other theory is that context is not being referenced properly thus why I resorted to using Signals 

 

Link to comment
Share on other sites

Ok 

 

i'm not sure why but I had to do the following in order to get it to work.

 

pauseUpdate()
	{
			if(this.unpaused) {
				delete this.unpaused;
				this.game.paused = false;
				console.log('unpaused');
			}
	}

	startGame()
	{
			this.unpaused = true;
			this.game.paused = false;
			console.log('start game');
	}

 

Have to call game.paused = true twice.... the log at pausedUpdate() indicates that game.paused is being set back to true. Not sure why

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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