您现在的位置是:首页 > 电脑 > 

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

2025-07-17 12:42:09
为什么玩家试图从第二个场景返回到第一个场景时会卡住? 我有两个场景。遇到触发器时,我会在它们之间切换。但是当试图回到第二个场景的第一个场景时,玩家僵住了,不再移动了。我试过清理变量,但它似乎不起作用......控制台中没有错误。我该如何解决这个问题? 进一步说明: 想自由切换场景。在第二个场景中与触发器碰撞时加载第一个,或者在第二个场景中与触发器碰撞时加载第一

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

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

进一步说明:

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

这是客户端代码。

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

  init(data) {
    cole.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.();
    let self = this;
    (	currentPlayers	, players => {
      Object.keys(players).forEach(playerId => {
        if (playerId === this.socket.id) {
          addPlayer(this, players[playerId]);
        } else {
          addOtherPlayers(this, players[playerId]);
        }
      });
    });

    (	newPlayer	, playerInfo => {
      addOtherPlayers(this, playerInfo);
    });

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

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

    (	mouseSpawned	, mouseInfo => {
      ct 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
    ct trigger = this.add.rectangle(400, -12, 100, 100, 0xffffff).setDisplaySize(50, 50);
    // Define the collider after the player has been created
    this.physics.(trigger);
    trigger.body.setImmovable(true);
    this.physics.(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);
      }
      (	playerMovement	, { x: player.x, y: player.y });
    }
  }
}

class SecondScene extends Phaser.Scene {
  ctructor() {
    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.();
    let self = this;
    this.add.sprite(0, 0, 	background	).setOrigin(0, 0);

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

    (	newPlayer	, playerInfo => {
      addOtherPlayers(this, playerInfo);
    });

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

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

    (	mouseSpawned	, mouseInfo => {
      ct 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

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

    // Define the collider after the player has been created
    this.physics.(trigger);
    trigger.body.setImmovable(true);
    this.physics.(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);
      }

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

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

ct game = new Phaser.Game(config);
ct 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) {
  ct 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 = ...
。您必须在每个场景中创建播放器和/或传递必须共享的数据。

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

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/905359.html

相关标签:无
上传时间: 2024-06-03 22:07:50
留言与评论(共有 8 条评论)
本站网友 天天好b
29分钟前 发表
200); player.setCollideWorldBounds(true); } function addOtherPlayers(self
本站网友 郑州4s店
22分钟前 发表
您必须在每个场景中创建播放器和/或传递必须共享的数据
本站网友 三亚电影院
29分钟前 发表
400
本站网友 富矿
10分钟前 发表
{ x
本站网友 重庆最新房价
26分钟前 发表
player.y }); } } } class SecondScene extends Phaser.Scene { ctructor() { super({ key
本站网友 强力删除
23分钟前 发表
800
本站网友 风云外挂
30分钟前 发表
或者在第二个场景中与触发器碰撞时加载第一个