javascript - Phaser 图像未预加载

标签 javascript html game-engine flappy-bird-clone phaser-framework

如果有人使用过 Phaser,也许你可以帮助我修改我的代码。我正在尝试创建一个 Flappy Bird 克隆版本,到目前为止它运行得很好。然而,每当我第一次打开游戏时,管道的 Sprite 似乎都没有出现。我已经预加载了小鸟和管道 Sprite ,并且在第一次尝试时仅加载了小鸟 Sprite 。一旦游戏重新开始(当小鸟死亡时),管道就会正常加载。我正在使用 WAMP 托管本地服务器。

这是我的代码:

var game = new Phaser.Game(400, 490, Phaser.AUTO, 'game_div');


var main_state = {

    preload: function() { 
        this.game.stage.backgroundColor = '#66CCFF';
        this.game.load.image('pipe', 'assets/pipe.png');
        this.game.load.image('bird', 'assets/bird.png');
        this.pipes = game.add.group();
        this.pipes.createMultiple(20, 'pipe');
    },

    add_one_pipe: function(x, y) {
        var pipe = this.pipes.getFirstDead();
        pipe.reset(x, y);
        pipe.body.velocity.x = -200
        pipe.outOfBoundsKill = true;
    },

    add_row_of_pipes: function() {
        var hole = Math.floor(Math.random()*5) + 1;

        for (var i = 0; i < 8; i++) {
            if (i != hole && i != hole + 1) {
                this.add_one_pipe(400, i * 60 + 10);
            }
        }

        this.score += 1;
        this.label_score.content = this.score;
    },

    create: function() { 
        this.bird = this.game.add.sprite(100, 245, 'bird');
        this.bird.body.gravity.y = 1000;

        var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
        space_key.onDown.add(this.jump, this);

        this.timer = this.game.time.events.loop(1500, this.add_row_of_pipes, this);

        this.score = 0;
        var style = { font: "30px Arial", fill: "#ffffff" };
        this.label_score = this.game.add.text(20, 20, "0", style);
    },

    update: function() {
        if (this.bird.inWorld == false) {
            this.restart_game();
        }

        this.game.physics.overlap(this.bird, this.pipes, this.restart_game, null, this);
    },

    jump: function() {
        this.bird.body.velocity.y = -350;
    },

    restart_game: function() {
        this.game.time.events.remove(this.timer);
        this.game.state.start('main');
    },
};

// Add and start the 'main' state to start the game
game.state.add('main', main_state);  
game.state.start('main'); 

最佳答案

tutorial中所示,尝试将组的创建放在create函数中:

create: function() { 
    this.bird = this.game.add.sprite(100, 245, 'bird');
    this.bird.body.gravity.y = 1000;

    var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    space_key.onDown.add(this.jump, this);

    this.pipes = game.add.group();
    this.pipes.createMultiple(20, 'pipe');

    this.timer = this.game.time.events.loop(1500, this.add_row_of_pipes, this);

    this.score = 0;
    var style = { font: "30px Arial", fill: "#ffffff" };
    this.label_score = this.game.add.text(20, 20, "0", style);
},

关于javascript - Phaser 图像未预加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22265573/

相关文章:

ios - iOS棋盘游戏的3D引擎?

c++ - 实体组件系统 - 渲染方法

javascript - 仅将纯文本粘贴到可编辑的 div 中

javascript - 如何仅使用 JavaScript 将属性数据转换为小写?

asp.net - 没有时区信息的 Javascript ASP.net 日期格式 - 时区偏移

javascript - 将变量附加到 HTML 元素?

html - 对齐单行 CSS 上的列以创建切换标签

css - 图像未显示在 HTML5 线框中

javascript - 如何从定义函数而不是调用对象访问变量状态

c++ - 在 C++ 中存储和访问图 block 属性