用户:
Nomen查看:95 回复:8 评论:8 创建时间:2023-07-29T12:40:10

const p_first = world.querySelector('#弹跳板-1')
p_first.onEntityContact(({ other, axis }) => {
if (axis.y === -1) {
other.velocity.y =5
}
})
const p_Second = world.querySelector('#弹跳板-2')
p_Second.onEntityContact(({ other, axis }) => {
if (axis.y === -1) {
other.velocity.y =5
}
})
这个怎么优化啊大佬?
点赞0
评论
已经不需要做什么优化了,看起来不错,如果你由很多相同的跳板,你可以批量为他们添加事件,我发了一条回帖,你可以看看
Nomenconst ps = ['#弹跳板-1', '#弹跳板-2'];
ps.forEach(e => {
world.querySelector(e).onEntityContact(({ other, axis }) => {
if (axis.y === -1) {
other.velocity.y = 5
}
})
})点赞0
评论
谢谢大佬
大佬请问forEach和这个e是什么意思呢?
具体用法是啥意思?怎么搞这种?
forEach对于数组里面每一个元素执行一次操作,我这段代码将ps数组中所有的元素遍历一遍,然后将这个字符串搜索并且添加事件
懂了qwq
SCS_user_LoeheodVOQdeveloper.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number
点赞0
评论