如何在每个更新上重置的单独的CreateEfect上计算要应用于播放器的力
我有一个计数器,我想通过添加一些数字来积累一些数字。在每个更新中,它重置为0。我想将这些更新分开为单独的createefect
。但这无效。我应该如何更改逻辑,以便玩家顺利进行并在正确的时间停下来。哦,我没有提到这是用于计算物理系统上的应用力。因此,之后我将部队重置为零。因此计数器是要施加的结果力。
import { render } from "solid-js/web";
import { on, createSignal, createEffect } from "solid-js";
function Counter() {
const [update, setUpdate] = createSignal(0);
const addUpdate = () => setUpdate(update() + 1);
const [count, setCount] = createSignal(0);
createEffect(on(update, dt => {
setCount(0)
}))
createEffect(on(update, dt => {
if (dt % 10 < 3) {
setCount(_ => _ + 3)
}
}))
createEffect(on(update, dt => {
if (dt % 10 < 9) {
setCount(_ => _ + 1)
}
}))
// Expected output: `0 4, 1 4, 2 4, 3 1, 4 1, 5 1, 6 1, 7 1, 8 1, 9 1, 10 4, 11 4, 12 4, 13 1`
createEffect(on(update, () => {
console.log([update(), count()].join(' '))
}))
setInterval(() => {
addUpdate()
}, 500)
return (
<button type="button">
{count()}
</button>
);
}
render(() => <Counter />, document.getElementById("app"));
I have a counter, that I want to accumulate some numbers by adding to it. On each update it resets to 0. I wanted to separate these updates into separate createEffect
. But it doesn't work. How should I change my logic, so the player goes smoothly and stops at the right time. Oh I didn't mention this is for calculating the applied forces on a physics system. So I reset the forces to zero afterwards. So the counter is the result force to be applied.
import { render } from "solid-js/web";
import { on, createSignal, createEffect } from "solid-js";
function Counter() {
const [update, setUpdate] = createSignal(0);
const addUpdate = () => setUpdate(update() + 1);
const [count, setCount] = createSignal(0);
createEffect(on(update, dt => {
setCount(0)
}))
createEffect(on(update, dt => {
if (dt % 10 < 3) {
setCount(_ => _ + 3)
}
}))
createEffect(on(update, dt => {
if (dt % 10 < 9) {
setCount(_ => _ + 1)
}
}))
// Expected output: `0 4, 1 4, 2 4, 3 1, 4 1, 5 1, 6 1, 7 1, 8 1, 9 1, 10 4, 11 4, 12 4, 13 1`
createEffect(on(update, () => {
console.log([update(), count()].join(' '))
}))
setInterval(() => {
addUpdate()
}, 500)
return (
<button type="button">
{count()}
</button>
);
}
render(() => <Counter />, document.getElementById("app"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难通过查看代码来理解您要实现的目标。
效果在每个状态更新都运行。因此,您可以观看柜台,并相应地清洁间隔。
It is hard to understand what you are trying to achieve by looking at your code.
The effect runs with every state update. So, you can watch the counter, and clean the interval accordingly.