Moving | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Moving

I was creating a code. The code had a feature to fit the canvas to the screen. At the same time, I have a moving code. But it doesn't work. Could anyone correct my code? https://code.sololearn.com/WPzBZD7d6AFn/#html

10th Jan 2018, 9:06 PM
Zakariya
Zakariya - avatar
4 Answers
+ 6
As Kirk said, you had a problem with local and global scopes with you variables. To make them all global I declared all in the 2nd line of your code. They get initialized (get values assigned) in your init function. Then you need to make your move function global as well, therefore it has to be outside of the init function. Hope this helps, here the code 😇 https://code.sololearn.com/WtRKTbXhj949/?ref=app
10th Jan 2018, 9:29 PM
Martin Möhle
Martin Möhle - avatar
+ 4
Your move() function is inside your init() function, so it can't be found in the global scope. Note, indentation normally should make this a little easier to find, but I just used SoloLearn's brace highlighting to see where the other end was, when putting my cursor at the "{"
10th Jan 2018, 9:15 PM
Kirk Schafer
Kirk Schafer - avatar
+ 4
As specified by @Kirk, the move() function actually is unreachable from the "onclick" attribute of the <button>... You need to define it in the global scope or to dynamically set the "onclick" attribute from within the actual scope where the move() function is declared, but also, as the function use the variable "ctx" which is initialized in init() function (require document was loaded to be initialized), you must keep its definition in a shared scope from both functions ^^ Simplest way to fix your code, would be to just change a little your move() function definition, without moving it outside the main scope (function assigned to the onload event), but defining it as property of the "window" object (which is the global scope): window.move = function move(){ // second name is no more necessary /* ... */ }
10th Jan 2018, 9:31 PM
visph
visph - avatar
+ 1
thanks
10th Jan 2018, 9:50 PM
Zakariya
Zakariya - avatar