How to use object collision in a game? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to use object collision in a game?

Hey, im trying to make a little game.. But ill need to make object detection for the walls, but i cant get it too work.. https://code.sololearn.com/W9LodTtqQmyn/#js This is a code that works in https://codepen.io/zer0/pen/JxxYgQ but not in sololearn, can anyone help me with how thats possible?

4th Jul 2019, 5:04 PM
mine
mine - avatar
4 Answers
+ 2
It is an event like onclick. You have to put the canvas declaration inside it, and the functions that use it. I didn't look at your code that much, but it's something like this: let canvas, ctx; window.onload = () => { canvas = document.querySelector("canvas"); ctx = canvas.getContext("2d"); loop(); };
4th Jul 2019, 5:43 PM
Airree
Airree - avatar
+ 2
It works, but the problem is, that you want to select the canvas before the document loads. You can either use the window.onload function, or putting your JS at the bottom of the body: https://code.sololearn.com/WdO46w4SiZ73/?ref=app
4th Jul 2019, 5:32 PM
Airree
Airree - avatar
+ 1
Thanks alot!! it works :D
4th Jul 2019, 5:52 PM
mine
mine - avatar
0
Thanks alot! could you also explain how to use the onload? Since it will get a bit messy if its all in the html file..
4th Jul 2019, 5:39 PM
mine
mine - avatar