Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts

    Question Trying to new up the Game object

    Hi, how come this doesn't work when trying to new up the Game object:

    I get TypeError: undefined is not a function for var game = new Game();
    Code:
    define(["require", "exports", "GameObjects"], function(require, exports, __GameObjects__) {
        var GameObjects = __GameObjects__;
    
        $(document).ready(function () {
            var game = new Game();
            $(document).keydown(game.onKeyDown);
            $(document).keyup(game.onKeyUp);
            $(document).keyup(game.onKeyUp);
        });
        function collides(a, b) {
            return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
        }
        function handleCollisions() {
            this.playerBullets.forEach(function (bullet) {
                this.enemies.forEach(function (enemy) {
                    if(collides(bullet, enemy)) {
                        enemy.explode();
                        bullet.active = false;
                    }
                });
            });
        }
        var Game = (function () {
            function Game() {
                this.CANVAS_WIDTH = 400;
                this.CANVAS_HEIGHT = 400;
                this.FPS = 30;
                this.enemies = [];
                this.playerBullets = new Array[40]();
                this.canvas = document.getElementById('canvas');
                this.rightDown = false;
                this.leftDown = false;
                this.space = false;
                this.context2D = this.canvas.getContext("2d");
                this.canvas.width = this.CANVAS_WIDTH;
                this.canvas.height = this.CANVAS_HEIGHT;
                this.initGame();
            }
            Game.prototype.initGame = function () {
                var player = new GameObjects.GameObjects.Player();
                player.OnShoot = function (bullet) {
                    this.playerBullets.push(bullet);
                };
            };
            Game.prototype.onKeyDown = function (evt) {
                if(evt.keyCode == 39) {
                    this.rightDown = true;
                } else {
                    if(evt.keyCode == 37) {
    
    etc................
    Last edited by nikos101; 12-18-2012 at 07:17 PM.


  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Why do you have an opening "(" here?
    Code:
    var Game = (function() {
    I suspect this to be an immediately executed anonymous function. Do you return anything from it? Otherwise you won't be able to instantiate it!

  • #3
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    putting this at the bottom worked!

    $(document).ready(function () {



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •