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 9282405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:22:03+00:00 2026-06-18T18:22:03+00:00

I am attempting to make a HTML5/JavaScript game with EnchantJS which involves a 6×6

  • 0

I am attempting to make a HTML5/JavaScript game with EnchantJS which involves a 6×6 grid of colored squares. I got 1 image to show but rather than having 36×4 lines of code to display all of the images i wanted to move it all to a function. The code breaks at line 62 this.addChild(block);. I can’t seem to figure out why this is not working. I’ve spent the past days reading up on what functions are and how to call info between functions. I can’t figure out what I’m doing wrong and I can’t find answers from searching. I come from a purely Java and c++ background so I think I’m messing up the syntax and not understanding something.

enchant();

window.onload = function() {

    /*
     *  Game width and height.
     *  Note: Game scale is half so actual width=width/2 and 
     *  actual heigh it height/2.
     */
    var game = new Game(1280,768);

    /*
     *  Game Preload Vars
     */
    game.preload('res/bg2x.png',
     'res/block.png');

    game.fps = 30;
    game.scale = 0.5;

    game.onload = function() {

      console.log("Game Loaded");
      var scene = new SceneGame;
      game.pushScene(scene);

    }

    game.start();
    window.scrollTo(0, 0);

    var SceneGame = Class.create(Scene, {

      initialize: function() {

        var game, bg;
        Scene.apply(this);
        game = Game.instance;

        // Background
        bg = new Sprite(1280,768);
        bg.image = game.assets['res/bg2x.png'];

        // Populate Screen
        this.addChild(bg);

        gridBuilder(500,75);

      }

    });

    var gridBuilder = function(x,y) {

      this.x=x;
      this.y=y;

      block = new Block;
      block.x = x;
      block.y = y;
      this.block = block;

      this.addChild(block); // THIS IS WHERE THE ERROR IS OCCURING.

    };

    var Block = Class.create(Sprite, {

        // The player character.     
        initialize: function() {
         // 1 - Call superclass constructor
         Sprite.apply(this,[100, 100]);
         this.image = Game.instance.assets['res/block.png'];
         // 2 - Animate
         this.animationDuration = 0;
         this.addEventListener(Event.ENTER_FRAME, this.updateAnimation);
        },

        updateAnimation: function (evt) {        
          this.animationDuration += evt.elapsed * 0.001;       
          if (this.animationDuration >= 0.25) {
              this.frame = (this.frame + 1) % 4;
              this.animationDuration -= 0.25;
          }
        },

    });

}
  • 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-18T18:22:04+00:00Added an answer on June 18, 2026 at 6:22 pm

    gridBuilder is a function, nothing more. It has no addChild method. The reason addChild works in SceneGame is because SceneGame is created by Class.create which I assume has an addChild method inherently in it. Either create gridBuilder the same way you create the class SceneGame or rethink what you actually want gridBuilder to do. One way or the other, something you’re doing isn’t adding up and you need to think about what you’re actually expecting your JS to accomplish.

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

Sidebar

Related Questions

My problem: I'm making an HTML5 game and am attempting to make it faster
I'm attempting to make a simple linear text game that will display inside a
I am attempting to make a program in C which presents a GUI and
Using php coding, I'm attempting to make a script which will grab content from
I'm attempting to make a Google Map which allows the user to check off
I am attempting to make a simple, light editor in HTML5 but I do
Basically I am attempting to make an HTML5 audio player but want to do
While attempting to make a game using Canvas I noticed a few quirks on
I am attempting to make a page that uses html5 audio to loop a
Attempting to make a NSObject called 'Person' that will hold the login details for

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.