Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:09:25+00:00 2026-06-01T13:09:25+00:00

I’m in the process of learning HTML5 (and OO Javascript) and am trying to

  • 0

I’m in the process of learning HTML5 (and OO Javascript) and am trying to make a simple game engine but rendering to canvas works under very odd conditions. Here is a link to the page and here is my index.html:

<html>
<head>
    <script type="text/javascript">
        function load()
        {
            var game = new Game();
            game.start();   
        }
    </script>
    <title>Game</title>
    <style>
        canvas
        {
            outline:0;
            border:1px solid #000;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body onload="load()">
    <canvas id='c'></canvas>

    <script src="classes/Tuple.js"></script>
    <script src="classes/Rect.js"></script>
    <script src="classes/Sprite.js"></script>
    <script src="classes/Game.js"></script>
</body>
</html>

When first loaded, it will give an NS_ERROR_DOM_TYPE_MISMATCH_ERR Exception in Firefox 11.0 and a TypeError Exception in Chrome 18.0. To make it work:

Firefox 11.0

  • select the URL in the address bar and manually hit enter OR
  • click the refresh button OR
  • press F5.

Chrome 18.0

  • select the URL in the address bar and manually hit enter OR
  • click the refresh button OR
  • press F5 OR
  • press Shift/Ctrl + F5.

What I suspect – Just a guess but it seems like one or more of the .js files is not available/ready by the time it is called during the first page load. Having stepped through it a couple of times in Firebug it seems that the sprite images never get loaded on the first page request.

Perhaps some of the .js files are still being downloaded as the instantiation to Game() is being made, then cached, and are only all available by the second page load?

Any ideas? I’d be grateful for your take on it, thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:09:26+00:00Added an answer on June 1, 2026 at 1:09 pm

    I copied your files locally and did some testing. It looks to me like it’s not your JavaScript files that are unavailable, but your sprite.

    You’re not loading the PNG until you call Game.start. When you do:

    roadSpritesheetImage.src = "assets/sprites/player/playerSpriteSheet.png";
    

    This begins an asynchronous download of the image. The rest of the code is executed before the image has fully loaded, so when you’re slicing the image, you’re slicing it as if the src attribute were null.

    This is why you’re able to hit refresh and everything works as expected… the image is cached.

    You can sort of preempt the loading of your sprite by adding an image tag just before the canvas:

    <img src="assets/sprites/player/playerSpriteSheet.png" style="display:none;" />
    

    This would work because the body load event isn’t fired until content (including images) is fully loaded (I believe, I’d have to look this up). Then, if you assign this image to img.src, you will be referencing the cached image.

    Alternatively, you could bind the rest of the game functionality to img.onload. For example:

    self.loadSprites = function()
    {
        var roadSpritesheetImage = new Image();     
        roadSpritesheetImage.onload = function() { 
            self.entities.push(new Sprite("testTile", 
                roadSpritesheetImage, 
                new Tuple(16, 16), 
                new Rect(0, 0, 32, 16)));
            self.run();
        };
    
        roadSpritesheetImage.src = "assets/sprites/player/playerSpriteSheet.png";
    };
    
    self.run = function() {
        var drawSuccessful = false;
        drawSuccessful = self.entities[0].draw(self.context);
        self.entities[0].nextFrame();
    
        //if we managed to draw the Sprite successfully without any raised exceptions
        if(drawSuccessful) {
            setTimeout(self.run, 300);
        } else { alert("stopping execution of the game."); }
    };
    
    self.start = function()
    {
        self.loadSprites();
    };
    

    Doing this is a bit cleaner and it would guarantee you don’t have any race conditions with your resources.

    I had a similar issue with a jQuery plugin I created to draw an N-puzzle from a source image. I found this MDN documentation extremely useful at the time, although I ended up using sliced divs instead of a canvas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to render a haml file in a javascript response like so:
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.