Как исправить Uncaught TypeError в Phaser 3 для разработки игр - PullRequest
1 голос
/ 27 мая 2019

Я следую учебному пособию по космическим шутерам для игры, которую разрабатываю, изучая Javascript. В этом руководстве многие классы помещаются в один и тот же файл и накладываются друг на друга, но я читал, что это не очень хорошая практика, и у всех классов должен быть свой отдельный файл. Ссылка на учебник здесь ...

https://yorkcs.com/2019/02/08/build-a-space-shooter-with-phaser-3-4/

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

Uncaught TypeError: Невозможно прочитать свойство 'getFirstTick' с нулевым значением

Выдает ошибку в строке 21 врагов.js для этой строки кода ...

this.play( 'basicenemy' );

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

entities.js

class Entities extends Phaser.GameObjects.Sprite
{
  constructor(scene, x, y, key, type)
  {
    super(scene, x, y, key, type);
    this.scene = scene;
    this.scene.add.existing(this);
    this.scene.physics.world.enableBody(this, 0);
    this.setData("type", type);
    this.setData("isDead", false);
  }
}

enemies.js

class BasicEnemy extends Entities
{
  constructor(scene, x, y)
  {
    super(scene, x, y, 'basicenemy', 'BasicEnemy');
    this.body.velocity.y = Phaser.Math.Between(50, 100);
    this.shootTimer = this.scene.time.addEvent({
        delay: 1000,
        callback: function() {
          var laser = new EnemyLaser(
            this.scene,
            this.x,
            this.y
          );
          laser.setScale(this.scaleX);
          this.scene.enemyLasers.add(laser);
        },
        callbackScope: this,
        loop: true
      });
    this.play( 'basicenemy' );
  }
}

sceneMain.js

//The Bullet Group
    this.basicEnemy = this.add.group();
    this.enemyLasers = this.add.group();
    this.playerLasers = this.add.group();

    this.time.addEvent({
        delay: 100,
        callback: function() {
          var enemy = new BasicEnemy(
            this,
            Phaser.Math.Between(0, this.game.config.width),
            0
          );
          this.basicEnemy.add(enemy);
        },
        callbackScope: this,
        loop: true
      });

В sceneMain.js Я пытаюсь заставить врагов появляться из верхней части экрана с помощью этой строки ...

this.basicEnemy.add(enemy);

Любая помощь будет принята с благодарностью.

Ответы [ 2 ]

0 голосов
/ 29 мая 2019

О боже, я получил его на работу !!! Я просто удалил this.play () из врагов.js и добавил его в sceneMain.js. Это новый код, который работает! По какой-то причине он не хотел играть из класса Enemies, но он играет в sceneMain. Я подумал, что просто использовал бы код, который уже работал, чтобы вызвать трех статических врагов, которые не двигались, и использовал бы его, чтобы вызвать врагов, которые падают сверху, как показано в учебнике. Теперь у меня есть постоянные враги и враги, которые порождают и стреляют пулями!

//The Enemies and Bullet Group That Spawns from the Top
    this.basicEnemy = this.add.group();
    this.enemyLasers = this.add.group();
    this.playerLasers = this.add.group();

    this.time.addEvent({
        delay: 100,
        callback: function() 
        {
          var enemy = new BasicEnemy( this, Phaser.Math.Between(0, this.game.config.width), 0 );
          this.basicEnemy = this.physics.add.sprite(enemy);
          this.basicEnemy.play( 'badfly', this);
        },
        callbackScope: this,
        loop: true
      });
0 голосов
/ 29 мая 2019
class SceneMain extends Phaser.Scene 
{
constructor() 
{
    super('SceneMain');

}
preload()
//loads our images and sounds
{
    //this.load.image( "playership","images/blueship.png" );
    this.load.image( 'spaceback4','images/spaceback4.png' );
    this.load.spritesheet( 'blueships', 'images/blueships.png', { frameWidth: 73, frameHeight: 80 });
    this.load.spritesheet( 'blueshot', 'images/blueshipshot.png', { frameWidth: 20, frameHeight: 55 });
    this.load.spritesheet( 'basicenemy', 'images/basicenemy.png', { frameWidth: 65, frameHeight: 80 });

    this.load.audio( 'explosion', 'audio/explosion.mp3' );
    this.load.audio( 'laser', 'audio/lasershot.mp3' );
    this.load.audio( 'enemylaser', 'audio/enemylaser.mp3' );

}
create()
//define our objects 
{
    //The Starfield Background
    this.starfield = this.add.tileSprite( 0, 0, 1080, 900, "spaceback4" );   

    //The Grid System
    var gridConfig={ row:5, cols:5, scene:this };
    var alignGrid = new AlignGrid( gridConfig );
    //alignGrid.showNumbers();

    //The Score
    this.sb = new ScoreBox( {scene:this} );
    alignGrid.placeAtIndex( 2, this.sb );
    model.score = 0; 


    //The Player Ship
    this.playerShip = this.physics.add.sprite( 0,0, 'blueships' );
    this.anims.create({
        key: 'fly',
        frames: this.anims.generateFrameNames( 'blueships', {start: 0, end: 9 }),
        frameRate: 16,
        repeat: -1
    });
    this.playerShip.play( 'fly', this );
    alignGrid.placeAtIndex( 22, this.playerShip );
    this.playerShip.setCollideWorldBounds( true );

    //The Enemy Ships
    this.basicEnemy = this.physics.add.sprite( 0,0, 'basicenemy' )
    this.basicEnemy2 = this.physics.add.sprite( 0,0, 'basicenemy' )
    this.basicEnemy3 = this.physics.add.sprite( 0,0, 'basicenemy' )

    this.anims.create({
        key: 'badfly',
        frames: this.anims.generateFrameNames( 'basicenemy', {start: 0, end: 9 }),
        frameRate: 16,
        repeat: -1
    });
    this.basicEnemy.play( 'badfly', this );
    this.basicEnemy2.play( 'badfly', this );
    this.basicEnemy3.play( 'badfly', this );
    alignGrid.placeAtIndex( 6, this.basicEnemy );
    alignGrid.placeAtIndex( 7, this.basicEnemy2 );
    alignGrid.placeAtIndex( 8, this.basicEnemy3 );

    //The Bullet Group
    this.basicEnemy = this.add.group();
    this.enemyLasers = this.add.group();
    this.playerLasers = this.add.group();

    this.time.addEvent({
        delay: 100,
        callback: function() {
          var enemy = new BasicEnemy(
            this,
            Phaser.Math.Between(0, this.game.config.width),
            0
          );
          this.basicEnemy.add(enemy);
        },
        callbackScope: this,
        loop: true
      });

    //The Audio
    this.sfx = {
        laser: [
          this.sound.add( 'laser' ),
          this.sound.add( 'enemylaser' )
        ],
        explosion: this.sound.add( 'explosion' )
      };

    //The Starfield
    alignGrid.placeAtIndex( 12, this.starfield );

    //Controls
    this.cursors = this.input.keyboard.createCursorKeys();
    this.fireButton = this.input.keyboard.addKey(Phaser.Input.Keyboard.SPACE);

}
update()
//constant running loops 
{
    //Scroll the starfield
    this.starfield.tilePositionY += -1.5;

    //Player Movement
    this.playerShip.setAccelerationX( 0 );
    this.playerShip.setDragX( 300 );


    if (this.cursors.left.isDown)
    {
        this.playerShip.setAccelerationX (-300 );
    }
    if (this.cursors.right.isDown)
    {
        this.playerShip.setAccelerationX ( 300 );
    }

    //  Fire bullet
    /*if (this.fireButton.isDown || this.input.activePointer.isDown) 
    {
        this.fireBullet();
    }*/

}
...