为什么玩家试图从第二个场景返回到第一个场景时会卡住?

编程入门 行业动态 更新时间:2024-10-04 07:21:06

为什么玩家试图从第二个<a href=https://www.elefans.com/category/jswz/34/1769935.html style=场景返回到第一个场景时会卡住?"/>

为什么玩家试图从第二个场景返回到第一个场景时会卡住?

我有两个场景。遇到触发器时,我会在它们之间切换。但是当试图回到第二个场景的第一个场景时,玩家僵住了,不再移动了。我试过清理变量,但它似乎不起作用......控制台中没有错误。我该如何解决这个问题?

进一步说明:

想自由切换场景。在第二个场景中与触发器碰撞时加载第一个,或者在第二个场景中与触发器碰撞时加载第一个。但是在加载第二个场景后,如果与触发器发生碰撞,玩家只会冻结并且不会移动。

这是客户端代码。

class FirstScene extends Phaser.Scene {
  constructor() {
    super({ key: 'first' });
  }

  init(data) {
    console.log('Updating FirstScene');
  }

  preload() {
    this.load.image('player', 'assets/player.png');
    this.load.image('otherPlayer', 'assets/player.png');
    this.load.image('mouse', 'assets/mouse.png');
  }

  create() {
    cursors = this.input.keyboard.createCursorKeys();
    let self = this;
    socket.on('currentPlayers', players => {
      Object.keys(players).forEach(playerId => {
        if (playerId === this.socket.id) {
          addPlayer(this, players[playerId]);
        } else {
          addOtherPlayers(this, players[playerId]);
        }
      });
    });

    socket.on('newPlayer', playerInfo => {
      addOtherPlayers(this, playerInfo);
    });

    socket.on('disconnectPlayer', playerId => {
      otherPlayers[playerId].destroy();
      delete otherPlayers[playerId];
    });

    socket.on('playerMoved', playerInfo => {
      otherPlayers[playerInfo.playerId].x = playerInfo.x;
      otherPlayers[playerInfo.playerId].y = playerInfo.y;
    });

    socket.on('mouseSpawned', mouseInfo => {
      const mouse = this.add.sprite(mouseInfo.x, mouseInfo.y, 'mouse').setDisplaySize(50, 50);
      mouse.id = mouseInfo.id;
      mice[mouse.id] = mouse;
    });

    addPlayer(this, { x: 400, y: 500 }); // temp location
    const trigger = this.add.rectangle(400, -12, 100, 100, 0xffffff).setDisplaySize(50, 50);
    // Define the collider after the player has been created
    this.physics.add.existing(trigger);
    trigger.body.setImmovable(true);
    this.physics.add.collider(player, trigger, () => {
      this.scene.switch('second');   
    });
  }

  update() {
    if(player) {
      if (cursors.left.isDown) {
        player.setVelocityX(-160);
      } else if (cursors.right.isDown) {
        player.setVelocityX(160);
      } else {
        player.setVelocityX(0);
      }
      if (cursors.up.isDown) {
        player.setVelocityY(-160);
      } else if (cursors.down.isDown) {
         player.setVelocityY(160);
      } else {
        player.setVelocityY(0);
      }
      socket.emit('playerMovement', { x: player.x, y: player.y });
    }
  }
}

class SecondScene extends Phaser.Scene {
  constructor() {
    super({ key: 'second' });
  }

  preload() {
    this.load.image('player', 'assets/player.png');
    this.load.image('otherPlayer', 'assets/player.png');
    this.load.image('mouse', 'assets/mouse.png');
    this.load.image('background', 'assets/background.png');
  }

  create() {
    cursors = this.input.keyboard.createCursorKeys();
    let self = this;
    this.add.sprite(0, 0, 'background').setOrigin(0, 0);

    socket.on('currentPlayers', players => {
      Object.keys(players).forEach(playerId => {
        if (playerId === this.socket.id) {
          addPlayer(this, players[playerId]);
        } else {
          addOtherPlayers(this, players[playerId]);
        }
      });
    });

    socket.on('newPlayer', playerInfo => {
      addOtherPlayers(this, playerInfo);
    });

    socket.on('disconnectPlayer', playerId => {
      otherPlayers[playerId].destroy();
      delete otherPlayers[playerId];
    });

    socket.on('playerMoved', playerInfo => {
      otherPlayers[playerInfo.playerId].x = playerInfo.x;
      otherPlayers[playerInfo.playerId].y = playerInfo.y;
    });

    socket.on('mouseSpawned', mouseInfo => {
      const mouse = this.add.sprite(mouseInfo.x, mouseInfo.y, 'mouse').setDisplaySize(50, 50);
      mouse.id = mouseInfo.id;
      mice[mouse.id] = mouse;
    });

    addPlayer(this, { x: 400, y: 500 }); // temp location

    const trigger = this.add.rectangle(400, -12, 100, 100, 0xffffff).setDisplaySize(50, 50);

    // Define the collider after the player has been created
    this.physics.add.existing(trigger);
    trigger.body.setImmovable(true);
    this.physics.add.collider(player, trigger, () => {
      this.scene.switch('first');
    });
  }

  update() {
    if(player) {
      if (cursors.left.isDown) {
        player.setVelocityX(-160);
      } else if (cursors.right.isDown) {
        player.setVelocityX(160);
      } else {
        player.setVelocityX(0);
      }
      if (cursors.up.isDown) {
        player.setVelocityY(-160);
      } else if (cursors.down.isDown) {
         player.setVelocityY(160);
      } else {
        player.setVelocityY(0);
      }

      socket.emit('playerMovement', { x: player.x, y: player.y });
    }
  }
}

const config = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  physics: {
    default: 'arcade',
    arcade: {
      gravity: { y: 0 }
    }
  },
  scene: [FirstScene, SecondScene]
};

const game = new Phaser.Game(config);
const socket = io();
let cursors;
let otherPlayers = {};
let mice = {};
let player;

function addPlayer(self, playerInfo) {
  player = self.physics.add.image(playerInfo.x, playerInfo.y, 'player').setDisplaySize(200, 200);
  player.setCollideWorldBounds(true);
}

function addOtherPlayers(self, playerInfo) {
  const otherPlayer = self.add.sprite(playerInfo.x, playerInfo.y, 'otherPlayer').setDisplaySize(200, 200);
    otherPlayer.playerId = playerInfo.playerId;
    otherPlayers[playerInfo.playerId] = otherPlayer;
}
回答如下:

好吧,我假设错误发生是因为

player
是在
first
场景中创建/设置的,而不是在
second
场景中,并且在
update
second
函数中,因为您正在访问
 player
变量.

最好的解决方案是为玩家使用属性而不是全局变量。 喜欢

this.player = ...
。您必须在每个场景中创建播放器和/或传递必须共享的数据。

信息: 如果移相器游戏/应用程序 冻结,最好的解决方案是检查浏览器控制台是否有错误。像这样你可以找到问题的原因,相当快。

更多推荐

为什么玩家试图从第二个场景返回到第一个场景时会卡住?

本文发布于:2024-05-30 17:04:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:场景   第二个   时会   到第   玩家

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!