Hey everyone

So I attempting to recreated one of my game I created in flash to HTML5 with EnchantJS. I originally got my basic images to display but then because I have to have a grid of 6x6 squares, constantly typing everything over and over again gets annoying. So I attempted to move everything into a function but it is not working and I don't know how. Can anyone explain to me what is going wrong?

Btw, I know I'm only trying to display one square with the gridBuilder() function but my problem is that nothing is showing anymore.

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);

        };

        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;
              }
            },

        });

}

Update:

I did some debugging and it is telling me that the line this.addChild(block); within the gridBuilder(); function is where the error is.

Member Avatar for LastMitch

@pattmorter

I did some debugging and it is telling me that the line this.addChild(block); within the gridBuilder(); function is where the error is.

What error meaning what does it say?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.