Pause game when screen is rotated

Hello all,

I have the following code to pause my game when i rotate the screen. This way i can make sure the user is always in landscape mode. Everything seems to work because I see the console log posting Resume Game and Pause Game but the actually game is not posing. Everything that is moving relies on the update function.

var CheckScreenOrientation = pc.createScript('checkScreenOrientation');

CheckScreenOrientation.prototype.initialize = function() {
    this.paused = false;
        
    this.app.graphicsDevice.on('resizecanvas', function(width, height) {
        calculateDimensions(width, height);
    });
    
    function calculateDimensions(w, h){   
    w = window.innerWidth;
    h = window.innerHeight;

    if(w > h){
        console.log("Landscape");
        togglePaused (false);
    }
    else if (w === h){
        console.log("square");
        togglePaused (true);
    }
    else{
        console.log("Portrait");
        togglePaused (true);
    }
    }    
    
    function togglePaused(shouldPause){
        if (shouldPause === false) {
            this.pc.app.timeScale = 1;
            console.log("Resume Game");
        } else {
            this.pc.app.timeScale = 0;
            console.log("Pause Game");
        }
    }
};

and this is a snippet of one of the codes that is not pausing

MoveRoad.prototype.update = function(dt) {
   
    if (this.gameManager.script.gameManager._isRacing === true){

        this.entity.translate(0, 0, 0.1);       
        var pos = this.entity.getPosition();
        if(pos.z > 17.85){
            this.entity.setPosition(pos.x,pos.y,0.438);
        }
    }
};

Hi @kdarius,

Not sure if that’s the problem here, but it may help to structure your code to have the same context in all of your methods. I mean something like this:

var CheckScreenOrientation = pc.createScript("checkScreenOrientation");

CheckScreenOrientation.prototype.initialize = function () {
  this.paused = false;

  this.app.graphicsDevice.on(
    "resizecanvas",
    function (width, height) {
      this.calculateDimensions(width, height);
    },
    this
  );
};

CheckScreenOrientation.prototype.calculateDimensions = function (w, h) {
  w = window.innerWidth;
  h = window.innerHeight;

  if (w > h) {
    console.log("Landscape");
    this.togglePaused(false);
  } else if (w === h) {
    console.log("square");
    this.togglePaused(true);
  } else {
    console.log("Portrait");
    this.togglePaused(true);
  }
};

CheckScreenOrientation.prototype.togglePaused = function (shouldPause) {
  if (shouldPause === false) {
    this.app.timeScale = 1;
    console.log("Resume Game");
  } else {
    this.app.timeScale = 0;
    console.log("Pause Game");
  }
};

Check if this resolves your issue, and also put a console log in your update to make sure that the this.app.timeScale value has indeed changed.

1 Like

The second issue is that like Unity, timeScale affects dt but the code above is not dependent on dt. Update function is still called regardless of timeScale.

Changing it to something like:

MoveRoad.prototype.update = function(dt) {
   
    if (this.gameManager.script.gameManager._isRacing === true){

        this.entity.translate(0, 0, 0.5 * dt); // Note use of dt here     
        var pos = this.entity.getPosition();
        if(pos.z > 17.85){
            this.entity.setPosition(pos.x,pos.y,0.438);
        }
    }
};
1 Like

@Leonidas I changed the code to what you had but now it is not triggering landscape. It always says portrait when i rotate it.

@yaustar ohhhh ok that makes sense.

@yaustar my dt is around like .016. should I do an if dt > .0001 then have a variable which from 0 to 1 and mutliply by that variable?

The dt is the seconds between frames so. Over time it should be steady as it’s a velocity no matter what the frame rate is. IE over 1 second, if you are using dt, it should always have moved the same distance no matter the frame rate.

Before it was moving 0.1 unit per frame. So it was 10fps, it would have moved 1 unit per second. If it was 60, it be 6 units in a second.