猫史档案馆


【代码岛干货】摔伤代码

用户:1086DRRalsei1086DRRalsei查看:12 回复:10 评论:12 创建时间:2021-03-14T21:29:42


(可以应用到作品中)

world.onPlayerJoin(({ entity }) => {
    entity.player.fall = 0
});
(async function () {
    while (true) {
        world.querySelectorAll("player").forEach(async (entity) => {
            switch (entity.player.moveState) {
                case Box喵layerMoveState.FALL:
                    entity.player.fall += Math.random()
                    break;
            }
            entity.onVoxelContact(({ entity }) => {
                if (entity.player.fall < 8) {
                    entity.player.fall = 0;
                    return;
                }
                entity.hurt(entity.player.fall)
                entity.player.directMessage('[剩余HP:' + entity.hp + ']你受到了' + entity.player.fall + '点摔伤伤害')
                entity.player.fall = 0
                if (!entity.hp) {
                    world.say(`${entity.player.name}摔,卒`)
                }
            });
        });
        await sleep(60)
    }
})()


回复

上一页1 页 / 共 1下一页
没尾巴的鲸鱼没尾巴的鲸鱼

你不能用while True,你要用onTick。你可能不知道为什么,我可以告诉你:两个事件可以嵌套使用,事件传回的属性(如ontick,tick就是属性)不受作用域影响,如果用while true喵循环,那么代码岛3.0的服务器处理不了这么多的指令,并且,onTick会在服务器因为卡顿而跳过某个tick是也一起跳过这次事件,也就是说,onTick起到了调节的作用。

点赞1


评论


没尾巴的鲸鱼没尾巴的鲸鱼

还有就是,你为什么要用异步函数?

 

点赞1


评论


没尾巴的鲸鱼没尾巴的鲸鱼

并且,你可以让每个玩家都创建一个onTick事件处理器,这样效率高,不用遍历。

点赞1


评论


没尾巴的鲸鱼没尾巴的鲸鱼

你好好的,只有一个分支,你为什么要用switch而不是用if?

点赞1


评论


ZNS_哪吒ZNS_哪吒

鹅(⊙o⊙)…

点赞0


评论


没尾巴的鲸鱼没尾巴的鲸鱼

//正确代码:
world.onPlayerJoin(({ entity }) => {
    entity.player.fall = 0
    world.onTick(() => {
        if (entity.player.moveState === Box喵layerMoveState.FALL) {
            entity.player.fall += Math.random()
            break;
        }
        entity.onVoxelContact(({ entity }) => {
            if (entity.player.fall < 4) {
                entity.player.fall = 0;
                return;
            }
            entity.hurt(entity.player.fall)
            entity.player.directMessage('[剩余HP:' + entity.hp + ']你受到了' + entity.player.fall + '点摔伤伤害')
            entity.player.fall = 0
            if (!entity.hp) {
                world.say(`${entity.player.name}摔,卒`)
            }
        });
    });
    await sleep(60);
});

点赞0


评论


幽梦子曦幽梦子曦

你没开启伤害

点赞0


评论


幽梦子曦幽梦子曦

 

world.onPlayerJoin(({ entity }) => {
    entity.enableDamage = true
    entity.player.fall = 0
});
(async function () {
    while (true) {
        world.querySelectorAll("player").forEach(async (entity) => {
            switch (entity.player.moveState) {
                case Box喵layerMoveState.FALL:
                    entity.player.fall += Math.random()
                    break;
            }
            entity.onVoxelContact(({ entity }) => {
                if (entity.player.fall < 9) {
                    entity.player.fall = 0;
                    return;
                }
                entity.hurt(entity.player.fall)
                entity.player.directMessage('[剩余HP:' + entity.hp + ']你受到了' + entity.player.fall + '点摔伤伤害')
                entity.player.fall = 0
                if (!entity.hp) {
                    world.say(`${entity.player.name}摔,卒`)
                }
            });
        });
        await sleep(60)
    }
})()

点赞0


评论


高贵的木叶龙A35d高贵的木叶龙A35d

保证卡喵,别问我为啥,我的未来战争一上线就被这类代码卡喵了,最后还是叫了搬砖喵(别问我为啥能叫官方来修)才修好

点赞0


评论


科技改变时代科技改变时代

world.onVoxelContact(async({entity,x,y,z,voxel})=>{
    if (!entity.isPlayer) return;
    let i=entity.player.fall-y
    if(entity.player.fall-y>15){
        entity.hurt(i)
        entity.player.directMessage('你受到了'+i+'点摔落伤害,目前血量是:'+entity.hp)
    }
    entity.player.fall=y
})

点赞0


评论