【问题标题】:phaser.js, javascript, Uncaught TypeError: Cannot read property 'forEach' of undefinedphaser.js,javascript,未捕获类型错误:无法读取未定义的属性“forEach”
【发布时间】:2015-11-20 16:35:42
【问题描述】:

我是 Phaser.js 和 Javascript 的初学者。我正在制作一个游戏,但我有一个错误,我不知道如何修复它。 我想我可以解释为什么会这样:当我调试 phisics 组 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 是未定义的,我不明白为什么以及我缺少什么。

感谢您的帮助。

【问题讨论】:

  • 您从未真正定义过this.game。在构造函数中尝试:this.game = game

标签: javascript phaser-framework


【解决方案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 () {
};

【讨论】:

  • 感谢您回答我,但这并不能解决错误,我的意思是当我输入 this.SpritesPlatform 时,即使我不运行,代码也不会运行并给我同样的错误'不要在 update() 中包含给我错误的行,请问我还遗漏了什么吗??
  • 我特别收到了这个错误:Uncaught ReferenceError: SpritesPlatform is not defined,在这一行:a = SpritesPlatform .create(0, 150, 'sprite');
  • 我最终通过在使用“SpritesPlatform”的任何地方添加“this”来修复它......
  • 您是否已将SpritesPlatform.... 更改为this.SpritesPlatform?检查this 以了解我的意思编辑: 好的!
猜你喜欢
  • 2021-11-24
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 2021-05-10
  • 2018-10-29
  • 2016-01-01
  • 2012-07-04
  • 2021-01-16
相关资源
最近更新 更多