Jump to content

State transition (beginner question)


123shansun
 Share

Recommended Posts

Hi,

There are five states in my demo game.

 

when the game is in play state

---> if the game is paused

      ---> if menu is clicked

            ---> then go to menu state (but game freezes here :()

      --->else

            --->destroy menu image and unpause the game.(this part works perfectly :()

 

I have attched a sample file.

It is based on phaser examples --> misc --> pause menu

 

Thank you!! :)

sample.txt

Link to comment
Share on other sites

I think it freezes because you paused the game. There's no listeners for the menu item.

Try to put this code in your create function:

// Identify your menu sprite here (this line)menu.inputEnabled = true;menu.events.onInputUp.add(function () {  game.state.start('Menu');});

Not so sure it'll work but if it doesn't it must be something similar. When your game is paused your if statements doesn't run.

Link to comment
Share on other sites

Thanks for the reply Guys!!

 

can you ever go to Menu state, for example from the preloader state. If yes, post your Menu state code here. you don't need to attach files, just paste it here.

 
This is the code for going from preload to menu
Game.Load.prototype = {
    preload: function() {
....
....
....       
    },
    create: function() {
        game.state.start('Menu'); // this is used to go from preload to menu
    }
};
------------------------
And this is the code in menu state for going to play state
 
update: function() {
 
        if (this.cursor.up.isDown)
            this.game.state.start('Play');
 
    }

-----------------------

I think it freezes because you paused the game. There's no listeners for the menu item.

Try to put this code in your create function:

// Identify your menu sprite here (this line)menu.inputEnabled = true;menu.events.onInputUp.add(function () {  game.state.start('Menu');});

Not so sure it'll work but if it doesn't it must be something similar. When your game is paused your if statements doesn't run.

This is the code in play state
 
game.input.onDown.add(unpause, self);
 
        //method that handles the pause menu
        function unpause(event) {
            // Only act if paused
            if (game.paused) {
                // Calculate the corners of the menu
                var x1 = w / 2 - 270 / 2, x2 = w / 2 + 270 / 2,
                        y1 = h / 2 - 180 / 2, y2 = h / 2 + 180 / 2;
 
                // Check if the click was inside the menu
if (event.x > x1 && event.x < x2 && event.y > y1 && event.y < y2) {
//if menu area is clicked go to main menu
menu.events.onInputUp.add(function () {
game.state.start('Menu');//this is the only part that doesn't work and I don't know what I'm doing wrong
});
                   
                }
                else {
menu.destroy();
game.paused = false;
 
                }
            }
--------------------------
Thanks for the help guys ! :)
Link to comment
Share on other sites

I think for what you're doing you should consider pausing the game in another way. Since a lot of things won't work if game.paused === true.

You can check this Handy tutorial by Loopeex You can see how he pauses the game without game.paused === true. In his way things will run normally and nothing will stop since game won't freeze.

 

 

I don't think this function would work if game is paused. Try the tutorial better. (In the Phaser example there wasn't such a line. Since it's hard to do it.)

menu.events.onInputUp.add(function () {game.state.start('Menu');});
Link to comment
Share on other sites

 

I think for what you're doing you should consider pausing the game in another way. Since a lot of things won't work if game.paused === true.

You can check this Handy tutorial by Loopeex You can see how he pauses the game without game.paused === true. In his way things will run normally and nothing will stop since game won't freeze.

 

 

I don't think this function would work if game is paused. Try the tutorial better. (In the Phaser example there wasn't such a line. Since it's hard to do it.)

menu.events.onInputUp.add(function () {game.state.start('Menu');});

 

Thanks Hady Hayman !!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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