猫史档案馆


玩家互动系统代码公开!

用户:Mini_editMini_edit查看:0 回复:1 评论:0 创建时间:2022-11-12T20:40:10


大家期待的玩家互动代码来了!

//玩家互动系统
world.onPlayerJoin(({ entity }) => {
    entity.player.message1 = "";
    entity.player.interactTimes = 0;
    entity.enableInteract = true;//设置所有玩家互动
    entity.interactRadius = 3;//互动格数为三格
    entity.interactHint = "与@" + entity.player.name + "@互动";//玩家头顶上会冒出提示语
    entity.onInteract(async ({ entity, targetEntity }) => {//新建对话框
        const result = await entity.player.dialog({//新建对话框
            type: Box3DialogType.SELECT,//设置对话框类型为选择框
            title: '访问' + targetEntity.player.name + "的个人主页",//对话框标题。通常对应是讲话人的名字。
            content: `和@ ${targetEntity.player.name} 互动\n该用户唯一识别码:${targetEntity.player.userKey}`,
            options: ['和TA私聊','退出互动'],   // 将提供玩家选择的选项放入数组里。
        })
        // 如果玩家点击了屏幕其他区域,取消了对话框。
        if (!result || result === null) {//判断玩家点击了屏幕其他区域,取消了对话框。
            entity.player.directMessage('已为你自动取消互动!');//向玩家发送私聊消息:'已为你自动取消互动!'
            return;//回来
        }
        if (result.index == 0) {//选择【和TA私聊】按钮
            const dialog = await entity.player.dialog({
                type: Box3DialogType.INPUT,//设置对话框类型为输入框
                title: "向 " + targetEntity.player.name + " 发送私聊信息",//对话框标题。通常对应是讲话人的名字。
                content: `${entity.player.name},请输入私聊内容`,//输入框提示语
                confirmText: '发送消息', // 确定按钮上面的文字。按下确定按钮后,提交回答。
                placeholder: '请输入私聊内容', // 输入框背景上的提示文字。
            });
            targetEntity.player.message1 = dialog;
            targetEntity.player.directMessage('收到 ' + entity.player.name + ' 向你发送的私聊信息,正在为你打开查看……')//告诉接收消息的玩家收到 ' + entity.player.name + ' 向你发送的私聊信息,正在为你打开查看……
            await sleep(5000);//等待5秒接收时间
            targetEntity.player.directMessage('正在查收 ' + entity.player.name + " 发送的私聊信息")//告诉接收消息的玩家'正在查收 ' + entity.player.name + " 发送的私聊信息"
            entity.player.directMessage('对方已收到聊天信息')//告诉发送消息的玩家:'对方已收到聊天信息'
            if (dialog) {
                const dialog = targetEntity.player.dialog({//新建对话框
                    type: Box3DialogType.TEXT,//设置对话框类型为对话框
                    title: "来自" + entity.player.name + "的留言",    // 对话框标题。通常对应是讲话人的名字。
                    content: targetEntity.player.message1, // 对话框内容。也就是对话正在讲什么。
                });
            }
        } else if (result.index == 1) {//选择【退出互动】按钮
            //因为是退出,所以没有代码
                    }
        }
    )
})



回复

上一页1 页 / 共 1下一页
令人闻风丧胆的赛车手令人闻风丧胆的赛车手

sofa

点赞0


评论