【问题标题】:Change the Black background loading screen to image (Phaser)将黑色背景加载屏幕更改为图像(Phaser)
【发布时间】:2021-12-29 00:29:01
【问题描述】:

我是 Phaser 的新手,我已经显示了加载屏幕,但我想在进度加载出现时将黑色背景更改为图像。但是我不确定我将如何做到这一点,因为我对使用的元素并不熟悉。无论如何我可以改变它吗?有人可以指导我吗?谢谢你。

这是代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <script src="//cdn.jsdelivr.net/npm/phaser@3.0.0/dist/phaser.min.js"></script>
    <script type="text/javascript">
      var config = {
        type: Phaser.AUTO,
        parent: "phaser-example",
        width: 800,
        height: 600,
        scene: {
          preload: preload,
          create: create
        }
      };
      var game = new Phaser.Game(config);
      function preload() {
        var progressBar = this.add.graphics();
        var progressBox = this.add.graphics();
        progressBox.fillStyle(0x222222, 0.8);
        progressBox.fillRect(240, 270, 320, 50);

        var width = this.cameras.main.width;
        var height = this.cameras.main.height;
        var loadingText = this.make.text({
          x: width / 2,
          y: height / 2 - 50,
          text: "Loading...",
          style: {
            font: "20px monospace",
            fill: "#ffffff"
          }
        });
        loadingText.setOrigin(0.5, 0.5);

        var percentText = this.make.text({
          x: width / 2,
          y: height / 2 - 5,
          text: "0%",
          style: {
            font: "18px monospace",
            fill: "#ffffff"
          }
        });
        percentText.setOrigin(0.5, 0.5);

        var assetText = this.make.text({
          x: width / 2,
          y: height / 2 + 50,
          text: "",
          style: {
            font: "18px monospace",
            fill: "#ffffff"
          }
        });
        assetText.setOrigin(0.5, 0.5);

        this.load.on("progress", function (value) {
          percentText.setText(parseInt(value * 100) + "%");
          progressBar.clear();
          progressBar.fillStyle(0xffffff, 1);
          progressBar.fillRect(250, 280, 300 * value, 30);
        });

        this.load.on("fileprogress", function (file) {
          assetText.setText("Loading asset: " + file.key);
        });
        this.load.on("complete", function () {
          progressBar.destroy();
          progressBox.destroy();
          loadingText.destroy();
          percentText.destroy();
          assetText.destroy();
        });

        this.load.image(
          "logo",
          "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_logo.svg.png"
        );
        for (var i = 0; i < 5000; i++) {
          this.load.image(
            "logo" + i,
            "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_logo.svg.png"
          );
        }
      }
      function create() {
        var logo = this.add.image(400, 300, "logo");
      }
    </script>
  </body>
</html>

【问题讨论】:

    标签: phaser-framework


    【解决方案1】:

    在 Phaser 中还有很多方法可以做到这一点。 我能想到的禁食。

    1. 创建一个场景(在示例中为 preloadScene),仅加载加载场景所需的图像
    2. 创建真实的加载场景(在示例中为mainScene),在这里您可以将图像添加到场景中并显示它们,因为它们已经加载了。
    3. 启动preloadScene

    重要提示:您必须从 config - 对象中删除场景部分。

    这是一个工作演示:

    var config = {
                type: Phaser.AUTO,
                parent: "game",
                width: 800,
                height: 600
                /* REMOVE THE SCENE FROM HERE */
            };
    
            var game = new Phaser.Game(config);
    
            var mainScene = {
                preload: function preload() {
                    // Add the images to the Scene, because now they are loaded
                    var logo = this.add.image(400, 300, "logo");
                    var bg = this.add.image(400, 300, "bg");
    
                    var progressBar = this.add.graphics();
                    var progressBox = this.add.graphics();
                    progressBox.fillStyle(0x222222, 0.8);
                    progressBox.fillRect(240, 270, 320, 50);
    
                    var width = this.cameras.main.width;
                    var height = this.cameras.main.height;
                    var loadingText = this.make.text({
                        x: width / 2,
                        y: height / 2 - 50,
                        text: "Loading...",
                        style: {
                            font: "20px monospace",
                            fill: "#ffffff"
                        }
                    });
                    loadingText.setOrigin(0.5, 0.5);
    
                    var percentText = this.make.text({
                        x: width / 2,
                        y: height / 2 - 5,
                        text: "0%",
                        style: {
                            font: "18px monospace",
                            fill: "#ffffff"
                        }
                    });
                    percentText.setOrigin(0.5, 0.5);
    
                    var assetText = this.make.text({
                        x: width / 2,
                        y: height / 2 + 50,
                        text: "",
                        style: {
                            font: "18px monospace",
                            fill: "#ffffff"
                        }
                    });
                    assetText.setOrigin(0.5, 0.5);
    
                    this.load.on("progress", function (value) {
                        percentText.setText(parseInt(value * 100) + "%");
                        progressBar.clear();
                        progressBar.fillStyle(0xffffff, 1);
                        progressBar.fillRect(250, 280, 300 * value, 30);
                    });
    
                    this.load.on("fileprogress", function (file) {
                        assetText.setText("Loading asset: " + file.key);
                    });
                    this.load.on("complete", function () {
                        progressBar.destroy();
                        progressBox.destroy();
                        loadingText.destroy();
                        percentText.destroy();
                        assetText.destroy();
                    });
    
                    for (var i = 0; i < 3; i++) {
                        this.load.image(
                            "logo" + i,
                            "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_logo.svg.png"
                        );
                    }
                    }
            }
    
            var preloadScene = {
                preload: function preload() {
                    // Load only images needed for the Loading Screen (keep it small)
                    this.load.image("logo", "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_logo.svg.png");
                    this.load.image( "bg", "https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/IC1024_-_SDSS_DR14.jpg/600px-IC1024_-_SDSS_DR14.jpg");
                },
                create: function create() {
                    // Start loading Scene
                    this.scene.start('Main')
                }
            }
    
            // ADD A Scene that load images needed for the real load Screen
            // these should be small, or the user will see a black screen for a long time
            game.scene.add('PerLoad', preloadScene);
    
            // ADD the Loading Scene/Screen
            game.scene.add('Main', mainScene);
            
            // Start The PreLoad Scene
            game.scene.start('PerLoad');
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <script src="//cdn.jsdelivr.net/npm/phaser@3.0.0/dist/phaser.min.js"></script>
      </head>
      <body>
        <div id="game" style="padding:0;margin:0;height:800px"></div>
      </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 1970-01-01
      • 2015-11-16
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多