用户:
神岛吉吉喵查看:13 回复:36 评论:13 创建时间:2021-04-26T14:27:36

还记得“挑战32”颁奖典礼上吉吉喵控制的烟花效果吗?
原理很简单:是通过粒子效果实现的
今天就送上代码教程!
https://www.bilibili.com/video/BV1Tf4y1p7Ve
具体代码如下:
console.clear()
function particleSpread(entity, colorList, scale = 1) {
entity.particleRate = 550//粒子生成速率
entity.particleLifetime = 2//粒子存活时间为2秒
entity.particleColor = colorList//粒子的颜色列表
entity.particleSize = [15 * scale, 10 * scale, 5 * scale, 2 * scale, scale]//粒子在5个阶段的大小
entity.particleSizeSpread = 2 * scale//粒子的大小的随机变化幅度
entity.particleVelocitySpread = [20 * scale, 20 * scale, 20 * scale]//粒子XYZ方向速度的随机变化幅度
entity.particleAcceleration = [0, -10 * scale, 0]//粒子往下飘落模拟重力效果
}
const YELLOW = new Box3RGBColor(10, 10, 2)//黄
const CYAN = new Box3RGBColor(2, 10, 10)//青
const MAGENTA = new Box3RGBColor(10, 2, 10)//品红
const RED = new Box3RGBColor(10, 2, 2)//红
const GREEN = new Box3RGBColor(2, 10, 2)//绿
const BLUE = new Box3RGBColor(2, 2, 10)//蓝
//各个颜色5个粒子阶段的颜色列表
const ColorList = [
[YELLOW, YELLOW, YELLOW, YELLOW, YELLOW],
[CYAN, CYAN, CYAN, CYAN, CYAN],
[MAGENTA, MAGENTA, MAGENTA, MAGENTA, MAGENTA],
[RED, RED, RED, RED, RED],
[GREEN, GREEN, GREEN, GREEN, GREEN],
[BLUE, BLUE, BLUE, BLUE, BLUE],
]
function randomColor() {
return ColorList[Math.floor(ColorList.length * Math.random())] //随机选取一个颜色列表
}
const MeshScale = [1 / 16, 1 / 16, 1 / 16] //默认的模型缩放系数
async function particleShoot(entity) {
const fireball = world.createEntity({
bounds: [0, 0, 0], // 隐形实体默认大小是1x1x1的方块, 现在把它从方块缩成大小为0x0x0的1个点
collides: false, // 开启碰撞
gravity: false, // 不受重力影响
position: entity.position,
})
const color = randomColor()//随机颜色
particleSpread(fireball, color, 0.3) //粒子大小系数缩到0.3, 形成小火球
await sleep(100) // 等待0.1秒后升空
fireball.velocity.set(0, 1, 0) // 火球速度为每秒向上1格
await sleep(2000) //升空2秒后爆炸
fireball.velocity.set(0, 0, 0) // 火球停止运动
particleSpread(fireball, color, 1) // 系数增大到1, 让火球变大
await sleep(1500) //大火球等待1.5秒后消失
fireball.destroy()
}
const firework = world.querySelector('#新年烟花-1')
firework.enableInteract = true
firework.interactRadius = 3
firework.onInteract(async () => {
var n = 3 //每次
while (n-- > 0) {
particleShoot(firework)//在"新年烟花-1"实体的位置发射烟花
await sleep(2000)//每隔2秒射1发
}
})
其中很多的参数都可以根据你的地图实际调整
快在你的地图制作一场绚丽的烟花
截图放在评论区吧~

开发API文档:https://docs.box3.codemao.cn/
加入代码岛3.0官方内测Q喵
下面的二维码会扫出来什么呢?( =•ω•= )m

该账号已被封禁function c(r,g,b){return new Box3RGBColor(r/255,g/255,b/255)} console.clear() async function CreateFire(){ for(i=0;i<3;i++){ for(i=0;i<10;i++){ const a=world.createEntity({ id:'a', gravity:false, collides:false, position:new Box3Vector3(Math.random()*100,0,Math.random()*100), particleSize: [20,20,20], particleRate:1000, particleLifetime:1, particleColor:[c(0,0,0),c(Math.random()*255,Math.random()*255,Math.random()*255),c(255,255,255)], particleVelocity: new Box3Vector3(0, 0, 0), particleVelocitySpread: new Box3Vector3(15,15,15), particleDamping:-1, });a.addTag('a')} world.querySelectorAll('*').forEach(async(e)=>{ if(!e.hasTag('a')){return} if(!e.isPlayer){ for(i=0;i<500;i++){ e.position.y+=10 await sleep(500)}}}) await sleep(5000) world.querySelectorAll('*').forEach((e)=>{if(!e.hasTag('a')){return}if(!e.isPlayer){e.destroy()}}) await sleep(1000) } }
点赞0
评论
该账号已被封禁function c(r,g,b){return new Box3RGBColor(r/255,g/255,b/255)}
console.clear()
async function CreateFire(){
for(i=0;i<3;i++){
for(i=0;i<10;i++){
const a=world.createEntity({
id:'a',
gravity:false,
collides:false,
position:new Box3Vector3(Math.random()*100,0,Math.random()*100),
particleSize: [20,20,20],
particleRate:1000,
particleLifetime:1,
particleColor:[c(0,0,0),c(Math.random()*255,Math.random()*255,Math.random()*255),c(255,255,255)],
particleVelocity: new Box3Vector3(0, 0, 0),
particleVelocitySpread: new Box3Vector3(15,15,15),
particleDamping:-1,
});a.addTag('a')}
world.querySelectorAll('*').forEach(async(e)=>{
if(!e.hasTag('a')){return}
if(!e.isPlayer){
for(i=0;i<500;i++){
e.position.y+=10
await sleep(500)}}})
await sleep(5000)
world.querySelectorAll('*').forEach((e)=>{if(!e.hasTag('a')){return}if(!e.isPlayer){e.destroy()}})
await sleep(1000)
}
}点赞1
评论
async_functionasync function Fire() {//这是我的
const random1 = Math.random(), random2 = Math.random(), random3 = Math.random(), random4 = Math.random()
const fire = world.createEntity({
gravity: false,
collides: false,
position: new Box3Vector3(Math.random() * 128, 128, Math.random() * 128),
particleSize: [8, 6, 4, 2],
particleRate: 1,
particleLifetime: 0.07,
particleVelocity: new Box3Vector3(0, 0, 0),
particleVelocitySpread: new Box3Vector3(45, 45, 45),
particleDamping: -1,
particleRateSpread: 500,
particleColor: new Box3RGBColor(random1 * 40, random2 * 2, random3 * 2)
})
fire.particleRate = 0
for (let x = 0; x < 128; x++) {
await sleep(20)
fire.position.y++
}
await sleep(2560)
fire.a喵Tag('fire')
fire.particleRate = 1200
fire.particleLifetime = 1
if (random4 < 0.33) {
fire.particleColor = new Box3RGBColor(random1 * 40, random2 * 2, random3 * 2)//red
} else if (random4 < 0.66) {
fire.particleColor = new Box3RGBColor(random1 * 2, random2 * 30, random3 * 2)//green
} else {
fire.particleColor = new Box3RGBColor(random1 * 2, random2 * 2, random3 * 40)//blue
}
await sleep(5000)
world.querySelectorAll('.fire').forEach((e) => { if (!e.isPlayer) { e.destroy() } })
}点赞0
评论
无尽深空console.clear() functionparticleSpread(entity,colorList,scale=1){ entity.particleRate=550//粒子生成速率 entity.particleLifetime=2//粒子存活时间为2秒 entity.particleColor=colorList//粒子的颜色列表 entity.particleSize=[15*scale,10*scale,5*scale,2*scale,scale]//粒子在5个阶段的大小 entity.particleSizeSpread=2*scale//粒子的大小的随机变化幅度 entity.particleVelocitySpread=[20*scale,20*scale,20*scale]//粒子XYZ方向速度的随机变化幅度 entity.particleAcceleration=[0,-10*scale,0]//粒子往下飘落模拟重力效果 } constYELLOW=newBox3RGBColor(10,10,2)//黄 constCYAN=newBox3RGBColor(2,10,10)//青 constMAGENTA=newBox3RGBColor(10,2,10)//品红 constRED=newBox3RGBColor(10,2,2)//红 constGREEN=newBox3RGBColor(2,10,2)//绿 constBLUE=newBox3RGBColor(2,2,10)//蓝 //各个颜色5个粒子阶段的颜色列表 constColorList=[ [YELLOW,YELLOW,YELLOW,YELLOW,YELLOW], [CYAN,CYAN,CYAN,CYAN,CYAN], [MAGENTA,MAGENTA,MAGENTA,MAGENTA,MAGENTA], [RED,RED,RED,RED,RED], [GREEN,GREEN,GREEN,GREEN,GREEN], [BLUE,BLUE,BLUE,BLUE,BLUE], ] functionrandomColor(){ returnColorList[Math.floor(ColorList.length*Math.random())]//随机选取一个颜色列表 } constMeshScale=[1/16,1/16,1/16]//默认的模型缩放系数 asyncfunctionparticleShoot(entity){ constfireball=world.createEntity({ bounds:[0,0,0],//隐形实体默认大小是1x1x1的方块,现在把它从方块缩成大小为0x0x0的1个点 collides:false,//开启碰撞 gravity:false,//不受重力影响 position:entity.position, }) constcolor=randomColor()//随机颜色 particleSpread(fireball,color,0.3)//粒子大小系数缩到0.3,形成小火球 awaitsleep(100)//等待0.1秒后升空 fireball.velocity.set(0,1,0)//火球速度为每秒向上1格 awaitsleep(2000)//升空2秒后爆炸 fireball.velocity.set(0,0,0)//火球停止运动 particleSpread(fireball,color,1)//系数增大到1,让火球变大 awaitsleep(1500)//大火球等待1.5秒后消失 fireball.destroy() } constfirework=world.querySelector('#新年烟花-1') firework.enableInteract=true firework.interactRadius=3 firework.onInteract(async()=>{ varn=3//每次 while(n-->0){ particleShoot(firework)//在"新年烟花-1"实体的位置发射烟花 awaitsleep(2000)//每隔2秒射1发 } })
点赞0
评论
无尽深空console.clear() functionparticleSpread(entity,colorList,scale=1){ entity.particleRate=550//粒子生成速率 entity.particleLifetime=2//粒子存活时间为2秒 entity.particleColor=colorList//粒子的颜色列表 entity.particleSize=[15*scale,10*scale,5*scale,2*scale,scale]//粒子在5个阶段的大小 entity.particleSizeSpread=2*scale//粒子的大小的随机变化幅度 entity.particleVelocitySpread=[20*scale,20*scale,20*scale]//粒子XYZ方向速度的随机变化幅度 entity.particleAcceleration=[0,-10*scale,0]//粒子往下飘落模拟重力效果 } constYELLOW=newBox3RGBColor(10,10,2)//黄 constCYAN=newBox3RGBColor(2,10,10)//青 constMAGENTA=newBox3RGBColor(10,2,10)//品红 constRED=newBox3RGBColor(10,2,2)//红 constGREEN=newBox3RGBColor(2,10,2)//绿 constBLUE=newBox3RGBColor(2,2,10)//蓝 //各个颜色5个粒子阶段的颜色列表 constColorList=[ [YELLOW,YELLOW,YELLOW,YELLOW,YELLOW], [CYAN,CYAN,CYAN,CYAN,CYAN], [MAGENTA,MAGENTA,MAGENTA,MAGENTA,MAGENTA], [RED,RED,RED,RED,RED], [GREEN,GREEN,GREEN,GREEN,GREEN], [BLUE,BLUE,BLUE,BLUE,BLUE], ] functionrandomColor(){ returnColorList[Math.floor(ColorList.length*Math.random())]//随机选取一个颜色列表 } constMeshScale=[1/16,1/16,1/16]//默认的模型缩放系数 asyncfunctionparticleShoot(entity){ constfireball=world.createEntity({ bounds:[0,0,0],//隐形实体默认大小是1x1x1的方块,现在把它从方块缩成大小为0x0x0的1个点 collides:false,//开启碰撞 gravity:false,//不受重力影响 position:entity.position, }) constcolor=randomColor()//随机颜色 particleSpread(fireball,color,0.3)//粒子大小系数缩到0.3,形成小火球 awaitsleep(100)//等待0.1秒后升空 fireball.velocity.set(0,1,0)//火球速度为每秒向上1格 awaitsleep(2000)//升空2秒后爆炸 fireball.velocity.set(0,0,0)//火球停止运动 particleSpread(fireball,color,1)//系数增大到1,让火球变大 awaitsleep(1500)//大火球等待1.5秒后消失 fireball.destroy() } constfirework=world.querySelector('#新年烟花-1') firework.enableInteract=true firework.interactRadius=3 firework.onInteract(async()=>{ varn=3//每次 while(n-->0){ particleShoot(firework)//在"新年烟花-1"实体的位置发射烟花 awaitsleep(2000)//每隔2秒射1发 } })
点赞0
评论
./'console.clear() constDefaultScale=1/2 functionspawn(x,y,z,meshName){ constmouey=world.createEntity({ mesh:meshName, collides:true, fixed:true, gravity:false, meshScale:DefaultScale, position:[x,y,z], }) returnmouey } functionparticleSpread(entity,colorList,scale=1){ entity.particleRate=550//粒子生成速率 entity.particleLifetime=2//粒子存活时间为2秒 entity.particleColor=colorList//粒子的颜色列表 entity.particleSize=[15*scale,10*scale,5*scale,2*scale,scale]//粒子在5个阶段的大小 entity.particleSizeSpread=2*scale//粒子的大小的随机变化幅度 entity.particleVelocitySpread=[20*scale,20*scale,20*scale]//粒子XYZ方向速度的随机变化幅度 entity.particleAcceleration=[0,-10*scale,0]//粒子往下飘落模拟重力效果 } constYELLOW=newBox3RGBColor(10,10,2)//黄 constCYAN=newBox3RGBColor(2,10,10)//青 constMAGENTA=newBox3RGBColor(10,2,10)//品红 constRED=newBox3RGBColor(10,2,2)//红 constGREEN=newBox3RGBColor(2,10,2)//绿 constBLUE=newBox3RGBColor(2,2,10)//蓝 //各个颜色5个粒子阶段的颜色列表 constColorList=[ [YELLOW,YELLOW,YELLOW,YELLOW,YELLOW], [CYAN,CYAN,CYAN,CYAN,CYAN], [MAGENTA,MAGENTA,MAGENTA,MAGENTA,MAGENTA], [RED,RED,RED,RED,RED], [GREEN,GREEN,GREEN,GREEN,GREEN], [BLUE,BLUE,BLUE,BLUE,BLUE], ] functionrandomColor(){ returnColorList[Math.floor(ColorList.length*Math.random())]//随机选取一个颜色列表 } constMeshScale=[1/16,1/16,1/16]//默认的模型缩放系数 asyncfunctionparticleShoot(entity){ constfireball=world.createEntity({ bounds:[0,0,0],//隐形实体默认大小是1x1x1的方块,现在把它从方块缩成大小为0x0x0的1个点 collides:false,//开启碰撞 gravity:false,//不受重力影响 position:entity.position, }) constcolor=randomColor()//随机颜色 particleSpread(fireball,color,0.3)//粒子大小系数缩到0.3,形成小火球 awaitsleep(100)//等待0.1秒后升空 fireball.velocity.set(0,1,0)//火球速度为每秒向上1格 awaitsleep(2000)//升空2秒后爆炸 fireball.velocity.set(0,0,0)//火球停止运动 particleSpread(fireball,color,1)//系数增大到1,让火球变大 awaitsleep(1500)//大火球等待1.5秒后消失 fireball.destroy() } for(constfireworkofworld.querySelectorAll('#新年烟花-1')){ firework.enableInteract=true firework.enableDamage=true firework.interactRadius=3 firework.onInteract(async()=>{ varn=3//每次 firework.hurt(5) while(n-->0){ particleShoot(firework)//在"新年烟花-1"实体的位置发射烟花 awaitsleep(2000)//每隔2秒射1发 } }) firework.onTakeDamage(()=>{ firework.interactHint=`剩余可放烟花次数:${firework.hp/5}` firework.interactColor.set(0,0,1) }) firework.onDie(()=>{ firework.destroy() spawn(65.3,9.7,61.6,'mesh/新年烟花-1.vb') }) }
点赞0
评论
科技改变时代console.clear() functionparticleSpread(entity,colorList,scale=1){ entity.particleRate=550//粒子生成速率 entity.particleLifetime=2//粒子存活时间为2秒 entity.particleColor=colorList//粒子的颜色列表 entity.particleSize=[15*scale,10*scale,5*scale,2*scale,scale]//粒子在5个阶段的大小 entity.particleSizeSpread=2*scale//粒子的大小的随机变化幅度 entity.particleVelocitySpread=[20*scale,20*scale,20*scale]//粒子XYZ方向速度的随机变化幅度 entity.particleAcceleration=[0,-10*scale,0]//粒子往下飘落模拟重力效果 } constYELLOW=newBox3RGBColor(10,10,2)//黄 constCYAN=newBox3RGBColor(2,10,10)//青 constMAGENTA=newBox3RGBColor(10,2,10)//品红 constRED=newBox3RGBColor(10,2,2)//红 constGREEN=newBox3RGBColor(2,10,2)//绿 constBLUE=newBox3RGBColor(2,2,10)//蓝 //各个颜色5个粒子阶段的颜色列表 constColorList=[ [YELLOW,YELLOW,YELLOW,YELLOW,YELLOW], [CYAN,CYAN,CYAN,CYAN,CYAN], [MAGENTA,MAGENTA,MAGENTA,MAGENTA,MAGENTA], [RED,RED,RED,RED,RED], [GREEN,GREEN,GREEN,GREEN,GREEN], [BLUE,BLUE,BLUE,BLUE,BLUE], ] functionrandomColor(){ returnColorList[Math.floor(ColorList.length*Math.random())]//随机选取一个颜色列表 } constMeshScale=[1/16,1/16,1/16]//默认的模型缩放系数 asyncfunctionparticleShoot(entity){ constfireball=world.createEntity({ bounds:[0,0,0],//隐形实体默认大小是1x1x1的方块,现在把它从方块缩成大小为0x0x0的1个点 collides:false,//开启碰撞 gravity:false,//不受重力影响 position:entity.position, }) constcolor=randomColor()//随机颜色 particleSpread(fireball,color,0.3)//粒子大小系数缩到0.3,形成小火球 awaitsleep(100)//等待0.1秒后升空 fireball.velocity.set(0,1,0)//火球速度为每秒向上1格 awaitsleep(2000)//升空2秒后爆炸 fireball.velocity.set(0,0,0)//火球停止运动 particleSpread(fireball,color,1)//系数增大到1,让火球变大 awaitsleep(1500)//大火球等待1.5秒后消失 fireball.destroy() } constfirework=world.querySelector('#新年烟花-1') firework.enableInteract=true firework.interactRadius=3 firework.onInteract(async()=>{ varn=3//每次 while(n-->0){ particleShoot(firework)//在"新年烟花-1"实体的位置发射烟花 awaitsleep(2000)//每隔2秒射1发 } })
点赞0
评论
无尽深空console.clear() functionparticleSpread(entity,colorList,scale=1){ entity.particleRate=550//粒子生成速率 entity.particleLifetime=2//粒子存活时间为2秒 entity.particleColor=colorList//粒子的颜色列表 entity.particleSize=[15*scale,10*scale,5*scale,2*scale,scale]//粒子在5个阶段的大小 entity.particleSizeSpread=2*scale//粒子的大小的随机变化幅度 entity.particleVelocitySpread=[20*scale,20*scale,20*scale]//粒子XYZ方向速度的随机变化幅度 entity.particleAcceleration=[0,-10*scale,0]//粒子往下飘落模拟重力效果 } constYELLOW=newBox3RGBColor(10,10,2)//黄 constCYAN=newBox3RGBColor(2,10,10)//青 constMAGENTA=newBox3RGBColor(10,2,10)//品红 constRED=newBox3RGBColor(10,2,2)//红 constGREEN=newBox3RGBColor(2,10,2)//绿 constBLUE=newBox3RGBColor(2,2,10)//蓝 //各个颜色5个粒子阶段的颜色列表 constColorList=[ [YELLOW,YELLOW,YELLOW,YELLOW,YELLOW], [CYAN,CYAN,CYAN,CYAN,CYAN], [MAGENTA,MAGENTA,MAGENTA,MAGENTA,MAGENTA], [RED,RED,RED,RED,RED], [GREEN,GREEN,GREEN,GREEN,GREEN], [BLUE,BLUE,BLUE,BLUE,BLUE], ] functionrandomColor(){ returnColorList[Math.floor(ColorList.length*Math.random())]//随机选取一个颜色列表 } constMeshScale=[1/16,1/16,1/16]//默认的模型缩放系数 asyncfunctionparticleShoot(entity){ constfireball=world.createEntity({ bounds:[0,0,0],//隐形实体默认大小是1x1x1的方块,现在把它从方块缩成大小为0x0x0的1个点 collides:false,//开启碰撞 gravity:false,//不受重力影响 position:entity.position, }) constcolor=randomColor()//随机颜色 particleSpread(fireball,color,0.3)//粒子大小系数缩到0.3,形成小火球 awaitsleep(100)//等待0.1秒后升空 fireball.velocity.set(0,1,0)//火球速度为每秒向上1格 awaitsleep(2000)//升空2秒后爆炸 fireball.velocity.set(0,0,0)//火球停止运动 particleSpread(fireball,color,1)//系数增大到1,让火球变大 awaitsleep(1500)//大火球等待1.5秒后消失 fireball.destroy() } constfirework=world.querySelector('#新年烟花-1') firework.enableInteract=true firework.interactRadius=3 firework.onInteract(async()=>{ varn=3//每次 while(n-->0){ particleShoot(firework)//在"新年烟花-1"实体的位置发射烟花 awaitsleep(2000)//每隔2秒射1发 } })
点赞0
评论
Dream梦想天空c喵ole.clear()functionparticleSpread(entity,colorList,scale=1){entity.particleRate=550//粒子生成速率entity.particleLifetime=2//粒子存活时间为2秒entity.particleColor=colorList//粒子的颜色列表entity.particleSize=[15*scale,10*scale,5*scale,2*scale,scale]//粒子在5个阶段的大小entity.particleSizeSpread=2*scale//粒子的大小的随机变化幅度entity.particleVelocitySpread=[20*scale,20*scale,20*scale]//粒子XYZ方向速度的随机变化幅度entity.particleAcceleration=[0,-10*scale,0]//粒子往下飘落模拟重力效果}c喵tYELLOW=newBox3RGBColor(10,10,2)//黄c喵tCYAN=newBox3RGBColor(2,10,10)//青c喵tMAGENTA=newBox3RGBColor(10,2,10)//品红c喵tRED=newBox3RGBColor(10,2,2)//红c喵tGREEN=newBox3RGBColor(2,10,2)//绿c喵tBLUE=newBox3RGBColor(2,2,10)//蓝//各个颜色5个粒子阶段的颜色列表c喵tColorList=[[YELLOW,YELLOW,YELLOW,YELLOW,YELLOW],[CYAN,CYAN,CYAN,CYAN,CYAN],[MAGENTA,MAGENTA,MAGENTA,MAGENTA,MAGENTA],[RED,RED,RED,RED,RED],[GREEN,GREEN,GREEN,GREEN,GREEN],[BLUE,BLUE,BLUE,BLUE,BLUE],]functionrandomColor(){returnColorList[Math.floor(ColorList.length*Math.random())]//随机选取一个颜色列表}c喵tMeshScale=[1/16,1/16,1/16]//默认的模型缩放系数asyncfunctionparticleShoot(entity){c喵tfireball=world.createEntity({bounds:[0,0,0],//隐形实体默认大小是1x1x1的方块,现在把它从方块缩成大小为0x0x0的1个点collides:false,//开启碰撞gravity:false,//不受重力影响position:entity.position,})c喵tcolor=randomColor()//随机颜色particleSpread(fireball,color,0.3)//粒子大小系数缩到0.3,形成小火球awaitsleep(100)//等待0.1秒后升空fireball.velocity.set(0,1,0)//火球速度为每秒向上1格awaitsleep(2000)//升空2秒后爆炸fireball.velocity.set(0,0,0)//火球停止运动particleSpread(fireball,color,1)//系数增大到1,让火球变大awaitsleep(1500)//大火球等待1.5秒后消失fireball.destroy()}c喵tfirework=world.querySelector('#新年烟花-1')firework.enableInteract=truefirework.interactRadius=3firework.onInteract(async()=>{varn=3//每次while(n-->0){particleShoot(firework)//在"新年烟花-1"实体的位置发射烟花awaitsleep(2000)//每隔2秒射1发}})
点赞0
评论
北极熊xryconsole.clear()
function particleSpread(entity, colorList, scale = 1) {
entity.particleRate = 550//粒子生成速率
entity.particleLifetime = 2//粒子存活时间为2秒
entity.particleColor = colorList//粒子的颜色列表
entity.particleSize = [15 * scale, 10 * scale, 5 * scale, 2 * scale, scale]//粒子在5个阶段的大小
entity.particleSizeSpread = 2 * scale//粒子的大小的随机变化幅度
entity.particleVelocitySpread = [20 * scale, 20 * scale, 20 * scale]//粒子XYZ方向速度的随机变化幅度
entity.particleAcceleration = [0, -10 * scale, 0]//粒子往下飘落模拟重力效果
}
const YELLOW = new Box3RGBColor(10, 10, 2)//黄
const CYAN = new Box3RGBColor(2, 10, 10)//青
const MAGENTA = new Box3RGBColor(10, 2, 10)//品红
const RED = new Box3RGBColor(10, 2, 2)//红
const GREEN = new Box3RGBColor(2, 10, 2)//绿
const BLUE = new Box3RGBColor(2, 2, 10)//蓝
//各个颜色5个粒子阶段的颜色列表
const ColorList = [
[YELLOW, YELLOW, YELLOW, YELLOW, YELLOW],
[CYAN, CYAN, CYAN, CYAN, CYAN],
[MAGENTA, MAGENTA, MAGENTA, MAGENTA, MAGENTA],
[RED, RED, RED, RED, RED],
[GREEN, GREEN, GREEN, GREEN, GREEN],
[BLUE, BLUE, BLUE, BLUE, BLUE],
]
function randomColor() {
return ColorList[Math.floor(ColorList.length * Math.random())] //随机选取一个颜色列表
}
const MeshScale = [1 / 16, 1 / 16, 1 / 16] //默认的模型缩放系数
async function particleShoot(entity) {
const fireball = world.createEntity({
bounds: [0, 0, 0], // 隐形实体默认大小是1x1x1的方块, 现在把它从方块缩成大小为0x0x0的1个点
collides: false, // 开启碰撞
gravity: false, // 不受重力影响
position: entity.position,
})
const color = randomColor()//随机颜色
particleSpread(fireball, color, 0.3) //粒子大小系数缩到0.3, 形成小火球
await sleep(100) // 等待0.1秒后升空
fireball.velocity.set(0, 1, 0) // 火球速度为每秒向上1格
await sleep(2000) //升空2秒后爆炸
fireball.velocity.set(0, 0, 0) // 火球停止运动
particleSpread(fireball, color, 1) // 系数增大到1, 让火球变大
await sleep(1500) //大火球等待1.5秒后消失
fireball.destroy()
}
const firework = world.querySelector('#新年烟花-1')
firework.enableInteract = true
firework.interactRadius = 3
firework.onInteract(async () => {
var n = 20 //每次
while (n-- > 0) {
particleShoot(firework)
await sleep(4000)
}
})
还是有报错


点赞0
评论