rust buildgen requestAnimationFrameLoop无法在闭包内使用struct方法

rust buildgen requestAnimationFrameLoop无法在闭包内使用struct方法,rust,wasm-bindgen,Rust,Wasm Bindgen,我正在尝试创建一个requestAnimationFrame循环,它将为每个帧调用game.render()。我正在学习本教程- 结构游戏{ 状态:游戏状态, ctx:web_sys::CanvasRenderingContext2d } impl游戏{ fn渲染(自我:游戏){ self.ctx.begin_path(); self.ctx.arc(self.state.x,50.0,40.0,0.0,2.0*std::f64::consts::PI); self.ctx.stroke();

我正在尝试创建一个requestAnimationFrame循环,它将为每个帧调用
game.render()
。我正在学习本教程-


结构游戏{
状态:游戏状态,
ctx:web_sys::CanvasRenderingContext2d
}
impl游戏{
fn渲染(自我:游戏){
self.ctx.begin_path();
self.ctx.arc(self.state.x,50.0,40.0,0.0,2.0*std::f64::consts::PI);
self.ctx.stroke();
}
}
#[wasm_bindgen(start)]
pub fn run()->结果{
让game=init();
设f=Rc::new(RefCell::new(None));
设g=f.clone();
*g、 借用mut()=Some(Closure::wrap(Box::new)(move | |){
game.render();
请求动画帧(f.借用().作为参考().展开());
})(如方框所示);
请求动画帧(例如,借用().as_ref().unwrap());
好(())
}
fn init()->游戏{
让doc=document();
让canvas=doc.create_元素(“canvas”).unwrap();
canvas.set_属性(“宽度”,“800px”).unwrap();
canvas.set_属性(“高度”,“800px”).unwrap();
canvas.set_id(“fp canvas”);
body().append_child(&canvas).expect(“无法附加画布”);
游戏{
ctx:context(),
状态:游戏状态{
x:3.0
}
}
}
但它给出了以下错误-


error[E0277]: expected a `std::ops::FnMut<()>` closure, found `[closure@src/lib.rs:89:51: 92:6 game:Game, f:std::rc::Rc<std::cell::RefCell<std::option::Option<wasm_bindgen::closure::Closure<dyn std::ops::FnMut()>>>>]`
  --> src/lib.rs:89:42
   |
89 |       *g.borrow_mut() = Some(Closure::wrap(Box::new(move || {
   |  __________________________________________^
90 | |         game.render();
91 | |         request_animation_frame(f.borrow().as_ref().unwrap());
92 | |     }) as Box<dyn FnMut()>));
   | |______^ expected an `FnMut<()>` closure, found `[closure@src/lib.rs:89:51: 92:6 game:Game, f:std::rc::Rc<std::cell::RefCell<std::option::Option<wasm_bindgen::closure::Closure<dyn std::ops::FnMut()>>>>]`
   |
   = help: the trait `std::ops::FnMut<()>` is not implemented for `[closure@src/lib.rs:89:51: 92:6 game:Game, f:std::rc::Rc<std::cell::RefCell<std::option::Option<wasm_bindgen::closure::Closure<dyn std::ops::FnMut()>>>>]`
   = note: wrap the `[closure@src/lib.rs:89:51: 92:6 game:Game, f:std::rc::Rc<std::cell::RefCell<std::option::Option<wasm_bindgen::closure::Closure<dyn std::ops::FnMut()>>>>]` in a closure with no arguments: `|| { /* code */ }
   = note: required for the cast to the object type `dyn std::ops::FnMut()`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `fighting-pixel`.

To learn more, run the command again with --verbose.

错误[E0277]:应为'std::ops::FnMut'闭包,已找到`[closure@src/lib.rs:89:51:92:6游戏:游戏,f:std::rc::rc]`
-->src/lib.rs:89:42
|
89 |*g.借用| u mut()=Some(Closure::wrap(Box::new)(move ||){
|  __________________________________________^
90 | | game.render();
91 | |请求|动画(f.借用().作为|参考().展开()));
92 | |})作为方框);
|| uuuuuuu^应为'FnMut'闭包,找到`[closure@src/lib.rs:89:51:92:6游戏:游戏,f:std::rc::rc]`
|
=帮助:未为实现特性'std::ops::FnMut'`[closure@src/lib.rs:89:51:92:6游戏:游戏,f:std::rc::rc]`
=注意:将`[closure@src/lib.rs:89:51:92:6 game:game,f:std::rc::rc]`在没有参数的闭包中:` | |{/*code*/}
=注意:强制转换到对象类型'dyn std::ops::FnMut()时需要`
错误:由于上一个错误而中止
有关此错误的详细信息,请尝试“rustc--explain E0277”。
错误:无法编译“战斗像素”。
要了解更多信息,请使用--verbose再次运行该命令。
如果我注释掉
game.render()
,它会编译。但我想保持一个状态,它将被更新以创建动画。我做错了什么?为什么闭包不允许调用结构方法


提前感谢您的帮助。

我发现了问题。游戏结构将类似于-

impl游戏{
fn渲染(自我:&游戏){
self.ctx.begin_path();
self.ctx.arc(self.state.x,50.0,40.0,0.0,2.0*std::f64::consts::PI);
self.ctx.stroke();
}
}
忘记为
自我添加
&
符号。感谢Discord channel#wg wasm的Pauan#6666指出这一点

为我工作。 我在这里分享我的游戏开始循环阻塞功能

如果安装线位于末端:

draw_场景(&self.state,&self.gl_context).unwrap();
impl GameGl{pub fn start_loop(mut self)->结果{
//渲染循环
//别问我
设f=Rc::new(RefCell::new(None));
设g=f.clone();
常数FPS_THROTTLE:f64=1000.0/60.0;//毫秒/帧
让mut previous:f64=js_sys::Date::now();
*g、 借用mut()=Some(Closure::wrap(Box::new)(move | |){
请求动画帧(f.借用().作为参考().展开());
//获取时间(毫秒)
让现在=js_sys::Date::now();
//条款:必须工作还是必须工作?
//当前旋转角度为1 rad/sec
如果现在
“忘记放置符号(&symbol)”。它立即起作用,谢谢分享。