Reputation: 26
I am building a snake game in web assembly, after lot of code commenting and un-commenting, i am seeing where it is giving me error, its giving me error in window().set_timeout_with_callback_and_timeout_and_arguments_0 saying
Uncaught Error: expected a number argument, found undefined
at _assertNum (index_bg.js:246:1)
at __wbg_adapter_27 (index_bg.js:257:1)
at real (index_bg.js:214:1)
code snippet of the game loop:
fn game_loop(snake: Rc<RefCell<Snake>>, canvas: Rc<Canvas>, time: u32){
let cl = Closure::<dyn FnMut(_)>::new(move |_: i32|{
game_loop(snake.clone(), canvas.clone(), time);
snake.borrow_mut().update();
snake.borrow().draw(&canvas);
});
let _ = utils::window().set_timeout_with_callback_and_timeout_and_arguments_0(cl.as_ref().unchecked_ref(), 100);
}
game_loop(snake, Rc::new(canvas), 100);
github link: https://github.com/kshitijk83/snake-game
what i found: __wbg_adapter_27 is written like this->
function __wbg_adapter_27(arg0, arg1, arg2) {
_assertNum(arg0);
_assertNum(arg1);
_assertNum(arg2);
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hcf9afe44e302a4ef(arg0, arg1, arg2);
}
arg2 is undefined here it is expecting a num here.
I have tried set_timeout_with_callback_and_timeout_and_arguments_1 function passing it another argument it doesn't work. i have gone through the doc and set_timeout_with_callback_and_timeout_and_arguments_0 definition looks same to me.
Upvotes: 0
Views: 70
Reputation: 26
I went throught web-sys docs and i found that I forgot to call closure.forget(), and I was using bad _
in dyn Fn(_) which was giving time this error.
corrected code:
fn game_loop(snake: Rc<RefCell<Snake>>, canvas: Rc<Canvas>, time: u32){
let snake_clone = snake.clone();
let canvas_clone = canvas.clone();
let cl = Closure::<dyn Fn()>::new(move ||{
snake_clone.borrow_mut().update();
snake_clone.borrow().draw(&canvas_clone);
game_loop(snake_clone.clone(), canvas_clone.clone(), time);
});
let _ = utils::window().set_timeout_with_callback_and_timeout_and_arguments_0(cl.as_ref().clone().as_ref().unchecked_ref(), 100);
cl.forget();
}
Upvotes: 0