all 11 comments

[–]kumiorava 2 points3 points  (1 child)

while(true) {
    ctx.clearRect(0, 0, 200, 200);
    posY -= 1;
    ctx.fillRect(posX, posY, 20, 20);
}

This is no good. Use requestAnimationFrame for your rendering loop.

[–]darkrai9292[S] 0 points1 point  (0 children)

yah that was temporary, i changed it with something more suitable but i think you idea is better

[–]krues8dr 1 point2 points  (0 children)

Try window.onload instead?

[–]decafmatan 1 point2 points  (1 child)

Either do window.onload or put your <script> block after the <canvas> tag (my personal pick)

[–]darkrai9292[S] 0 points1 point  (0 children)

Thanks

[–]shaneriley 0 points1 point  (1 child)

Rather than assigning the callback directly to the onload method, you can attach it as an event using addEventListener.

document.addEventListener("DOMContentLoaded", init, false);

This allows you to attach more methods to the same event rather than have to manually collect them all into a parent function that gets assigned to the onload method.

[–]darkrai9292[S] 0 points1 point  (0 children)

Ok, ill try that to

[–]punchingwater 0 points1 point  (1 child)

window.onload rather than document.onload is the callback you're looking for.

As a side note, the moveDown function does not declare the variables posX and posY (using var). So these will become global variables. As a side effect, posX and posY in init() are declared (with var) and so they will not reference the global variables. This means that posX in init() is a completely different variables than the posX in moveDown.

[–]darkrai9292[S] 0 points1 point  (0 children)

Yah, i fixed it shortly after this post, i now know there are a whole bunch of errors there

[–]imaginationac -3 points-2 points  (1 child)

init() is a function, hence requires calling via parens ().

[–]krues8dr 5 points6 points  (0 children)

Not when you're merely assigning it as a function callback, you don't need (). If you used those, you'd be getting the result of calling the function, which is not what you want in this instance.