I followed Mozila's example on their web-site and tried to create a simple 2d game. Everything works fine. Then I decided to separate the code from the html page into a single js file. But for some unknown reason it isn't loaded into my html. here's my html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Gamedev Canvas Workshop</title>
<link rel="stylesheet" href="style.css"/>
<script src="Script.js"></script>
</head>
<body>
<canvas id="myCanvas" width="480" height="320"></canvas>
</body>
</html>
and here is the beginning of the script file:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var ballRadius = 10;
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 2;
var dy = -2;
var paddleHeight = 10;
var paddleWidth = 75;
var paddleX = (canvas.width-paddleWidth)/2;
var rightPressed = false;
var leftPressed = false;
var brickRowCount = 5;
Again everything works fine when I use inline script inside the html. But as soon as I try to separate the code and html - it fails. Any help?
` tag. That way, the canvas element exists and `getElementById` can find it. (Inline script would have the same problem.)
– T.J. Crowder Mar 09 '18 at 12:09