My images won't load in canvas.
I wanted to try my hand at making a game using JS. The games main loop seems to be running the array names images has the file path to the source material how ever I can't figure out how to get them to display. This is the JS file http://pastebin.com/m21D5Pz5 and the html <!DOCTYPE html>
<head> <link href='http://fonts.googleapis.com/css?family=Covered+By+Your+Grace' rel='stylesheet' type='text/css'> <link href='ooop.css' rel='stylesheet' type='text/css'> </head> <body> <div id="wrapper"> <div id="banner"> <h1 id="title">#OfficeOfOldPeople</h1> </div> <div class="content"> <canvas id="c1" width="600" height="600"></canvas><br />
<script src="test.js">
game.run();</script>
</div>
<div class="footer">
</div>
</div>
</body>
</html5>
Can some one tell me what I am doing wrong?
Posted Dec 16, 2014, 2:22 PM UTC
35
Functions and properties are case-sensitive. When images load, they check for an
onloadfunction and execute that - but you've definedonLoad, notonload.I tried both. Neither will work :(
OK, so there's a couple more problems, but they're pretty minor except for this scope issue.
The good news is that your image loader basically works.
You main issue is that your onload functions are defined in a
forloop, butforloops don't have a scope.When each image fires its
onloadfunction, it will be using the value of variablei. Theforloop has probably long finished by this point, so every image will be looking forimages[4](iis iterated at the end of the last loop).To fix this, we just have to use your
onloadfunction differently. ALoadEvent, like other Events, pass theEventobject as the first argument. If we name that, we can access the event'stargetproperty, which should be theimgelement:Your browser's developer tools will really help you. If you put the statement
debugger;in your code somewhere, you can step over each instruction and see how your variables change.Thank you! It works now :)