phaser.js, javascript, Uncaught TypeError: Невозможно прочитать свойство 'forEach' из неопределенного

Я новичок в Phaser.js и Javascript. Я делаю игру, и у меня есть ошибка, которую я не могу понять, как это исправить. Я думаю, что могу объяснить, почему это происходит: когда я отлаживаю физическую группу, SpritesPlatform показывает, что она не определена.. Я не знаю почему..

здесь файл, в котором у меня ошибка (я не могу поместить все приложение здесь, потому что там много файлов, но проблема касается только этого файла, а спрайты загружаются в другой файл).

файл game.js (main.js)

MyGame.Game = function (game) {
    this.game;      //  a reference to the currently running game (Phaser.Game)
    this.add;       //  used to add sprites, text, groups, etc (Phaser.GameObjectFactory)
    this.load;      //  for preloading assets (Phaser.Loader)
    this.stage;     //  the game stage (Phaser.Stage)
    this.state;     //  the state manager (Phaser.StateManager)
    this.world;     //  the game world (Phaser.World)
    this.physics;   //  the physics manager (Phaser.Physics)
};

MyGame.Game.prototype = {

    init: function () {

            this.game.renderer.renderSession.roundPixels = true;
            this.physics.startSystem(Phaser.Physics.ARCADE);
            this.physics.arcade.gravity.y = 800;

    },

    create: function () {

        // background color bleu
        this.stage.backgroundColor = 0x479cde;

        //add sprites into physics group
        SpritesPlatform = this.game.add.physicsGroup();
        a = SpritesPlatform.create(0, 150, 'sprite');
        a.scale.setTo(0.2, 0.2);
        b = SpritesPlatform.create(79, 187, 'sprite2');
        b.scale.setTo(0.2, 0.2);
        c = SpritesPlatform.create(300, 100, 'sprite3');
        c.scale.setTo(0.4, 0.4);

        SpritesPlatform.setAll('body.allowGravity', false);
        SpritesPlatform.setAll('body.immovable', true);
        SpritesPlatform.setAll('body.velocity.x', 150);

        //test to see if the group contains the sprites
        /*var i = 0;
        for (var i = 0, len = SpritesPlatform.children.length; i < len; i++) {
            console.log(SpritesPlatform.children[i]);
        }*/

        //debug
        console.log(this.game.SpritesPlatform); // prints undefined .. why???


  },

    // repeat the mouvement of the sprites when it crosses the screen
    wrapSprites: function (SpritesPlatform) {

           if (SpritesPlatform.body.velocity.x < 0 && SpritesPlatform.x <= -160)
            {
                SpritesPlatform.x = 800;
            }

        },


    update: function () {

        console.log(this.game.SpritesPlatform); // prints undefined  also of course... ?? why??
        //the error is here ... if i put this in comment the code will display the board and the sprite ...
        //this.game.SpritesPlatform.forEach(this.game.wrapSprites, this.game);
    },
};

проблема в функции обновления в строке: //this.game.SpritesPlatform.forEach(this.game.wrapSprites, this.game);

Похоже, что SpritesPlatform не определена, и я не понимаю, почему и чего мне не хватает.

спасибо за любую помощь.

1 ответ

Решение

Когда вы впервые создаете группу, вы должны объявить ее так:

this.SpritesPlatform = this.game.add.physicsGroup();

и когда вы хотите обратиться к этой группе, вы можете сделать это с console.log(this.SpritesPlatform)

Редактировать:

На самом деле вам не нужно ничего делать

MyGame.Game = function (game) {
  this.game;      //  a reference to the currently running game (Phaser.Game)
  this.add;       //  used to add sprites, text, groups, etc (Phaser.GameObjectFactory)
  this.load;      //  for preloading assets (Phaser.Loader)
  this.stage;     //  the game stage (Phaser.Stage)
  this.state;     //  the state manager (Phaser.StateManager)
  this.world;     //  the game world (Phaser.World)
  this.physics;   //  the physics manager (Phaser.Physics)
};

Все эти свойства легко доступны, поэтому вы можете просто использовать их в любом месте внутри состояния. Это должно работать просто отлично:

MyGame.Game = function () {
};
Другие вопросы по тегам