用户:
沙拉黄查看:0 回复:0 评论:0 创建时间:2022-06-21T07:31:17
这期的帖子较长,耐心看完,你也可能提升。(相对有基础的而言):
首先,pvp地图里的右键菜单代码
world.onPress(async ({ entity, button }) => {
if (button == 'action1') {
if (!entity.hp) { entity.player.forceRespawn();
entity.player.directMessage('复活成功'); return }
如果玩家为喵亡状态,那么先复活再说。以下为菜单代码:
const save = await entity.player.dialog({ type: 'select', title: `${entity.player.name} 的信息`, content: `玩家:${entity.player.name}\nhp:${entity.hp}\n分数:${entity.player.record}\n射程:${entity.player.maxDis}\n伤害:${entity.player.hurt}\n武器:${entity.player.knife.name}`, options: ['保存', '排行榜', '查询玩家信息', ' 操作大厅', '切换武器', '武器介绍', '刷新', '装备防御装备'] });
if (!save || save == null) return;
switch (save.index) {
这里有一个switch语句,准备去执行列表的句子。总共是大概有200行,我就不把一一的选项代码晾出来了,直接给你们看一下case 4,也就是切换武器。
const selectknivesType = await entity.player.dialog({ type: 'select', title: '选择攻击方式', content: '列表如下,更多敬请期待', options: ['点攻', '弹攻'] });
if (!selectknivesType || selectknivesType == null) return;
switch (selectknivesType.index) {
case 0:
类似的,定义了一个常量,赋值为选项框的选项。这个时候,我们要干什么大家心里很清楚吧:
case 0:
let knivesList = [];
let knives_list = [];
await knives.forEach((i) => {
console.log(i.name + '(需要' + i.score + '个积分)')
knivesList.push(i.name + '需要(' + i.score + ')个积分');
knives_list.push(i);
});
const selectknives01 = await entity.player.dialog({ type: 'select', title: '选择武器', content: '武器列表如下,更多敬请期待', options: [knivesList] });
if (!selectknives01 || selectknives01 == null) return;
这里,我是有一个对象,也放出来
const common = {};
common.name = '经典';
common.score = 0;
common.maxDis = 1.15;
common.beatBack = 0.7;
common.hurt = 1.32;
common.wait = 0.2;
const sniping = {};
sniping.name = '狙击';
sniping.score = 120;
sniping.maxDis = 2;
sniping.beatBack = 1.4;
sniping.hurt = 1.32;
sniping.wait = 5;
const beatBack = {};
beatBack.name = '击退';
beatBack.score = 460;
beatBack.maxDis = 0.4;
beatBack.beatBack = 2.5;
beatBack.hurt = 0.1;
beatBack.wait = 0.07;
const maxSniping = {};
maxSniping.name = '超级狙击';
maxSniping.score = 820;
maxSniping.maxDis = 3;
maxSniping.beatBack = 2;
maxSniping.hurt = 1.6;
maxSniping.wait = 10;
const knives = [
common,
sniping,
beatBack,
maxSniping
];
这些元素分别表示什么我就不细说了。遍历knives,把选项放入列表,再让玩家选择。至此,本帖的重点到来了。
if (entity.player.score < knives_list[selectknives01].score) {
await scoreError(knives_list[selectknives01].score, entity.player.name); return; }
entity.player.knife = knives_list[selectknives01];
entity.player.directMessage(`切换成功!`);
entity.player.knifeType = '点攻';
判断玩家有积分购买......但是我确切的告诉你,问题不在这里,问题在case 0 开始的地方,也就是上上个代码。
因为我用注释逼近法试了,唯独留下那一部分遍历会报错。
给大家看看报错
TypeError: str.charCodeAt is not a function
at utf8ToBytes (<isolated-vm>:21332:25) at encode (<isolated-vm>:21376:27) at MuWriteStream.writeString (<isolated-vm>:21613:46) at MuUTF8.diff (<isolated-vm>:21099:20) at MuArray.diff (<isolated-vm>:19042:24) at MuStruct.diff (<isolated-vm>:20855:31) at MuUnion.diff (<isolated-vm>:21033:24) at MuStruct.diff (<isolated-vm>:20855:31) at Object.result.<computed> [as dialog] (<isolated-vm>:16919:24) at (<isolated-vm>:14605:46) at send (/usr/src/app/build/start.js:268606:53) 非常好。 行数不给就算了,居然一句提示都没有? 那我怎么知道哪里有问题,还是说遍历...... 至少我看遍历那一段代码是没有问题的,你们呢? 哦,补充一句,选择‘点攻’之后的代码被报错强行截止了,弹不出来。 希望早日有人回复!