javascript Phaser3如何开始游戏水平(移动的)

v64noz0r  于 2023-09-29  发布在  Java
关注(0)|答案(1)|浏览(106)

我想开始我的平台游戏,将在Phaser 3中水平运行在移动的上,我该如何做?如果我不能直接水平启动,我是否可以检测用户是垂直还是水平握持手机并显示警告?
下面是我的config.ts类;

import { Types } from "phaser";
import baseGameConfig from "@games/config.base";
import Scenes from "./src/scenes";
export const DEFAULT_WIDTH: number = 1280;
export const DEFAULT_HEIGHT: number = 720;
export const MAX_WIDTH: number = 1920;
export const MAX_HEIGHT: number = 1080;
export let SCALE_MODE: "FIT" | "SMOOTH" = "SMOOTH";

const gameConfig: Types.Core.GameConfig = {
  ...baseGameConfig,
  title: "monkeygo",
  scene: Scenes.BootScene.scene,
  physics: {
    default: "arcade",
    arcade: {
      debug: process.env.NODE_ENV !== "production",
      gravity: {
        y: 1000,
      },
      fps: 30,
    },
  },
  scale: {
    mode: Phaser.Scale.NONE,
    autoCenter: Phaser.Scale.CENTER_BOTH,
    width: DEFAULT_WIDTH,
    height: DEFAULT_HEIGHT,
  },
};

export default gameConfig;
k4emjkb1

k4emjkb11#

您可以使用游戏配置的scale部分中的orientation属性设置方向。要使游戏水平运行,请将其设置为“横向”:

scale: {
  mode: Phaser.Scale.NONE,
  autoCenter: Phaser.Scale.CENTER_BOTH,
  width: DEFAULT_WIDTH,
  height: DEFAULT_HEIGHT,
  orientation: 'landscape', // Set the orientation to landscape
},

您可以侦听resize事件以检测方向更改并做出相应的响应。例如,您可以将以下代码添加到BootScene:

class BootScene extends Phaser.Scene {
  constructor() {
    super({ key: 'BootScene' });
  }

  preload() {
    this.load.image('warning', 'path/to/warning-image.png');
  }

  create() {
    this.scale.on('resize', this.handleResize, this);
    this.handleResize(); // Call it initially to set the correct orientation
  }

  handleResize() {
    if (this.scale.isGamePortrait) {
      // Show a warning message if the device is in portrait mode
      const warning = this.add.image(DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 2, 'warning');
      warning.setOrigin(0.5);
    }
  }
}

这段代码监听resize事件并检查游戏是否处于纵向模式。如果是,则显示警告消息。
希望这对你有帮助。

相关问题