Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-18-2012, 07:11 PM   PM User | #1
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
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..
nikos101 is offline   Reply With Quote
Old 12-18-2012, 07:40 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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!
devnull69 is offline   Reply With Quote
Old 12-20-2012, 12:25 PM   PM User | #3
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
putting this at the bottom worked!

$(document).ready(function () {
__________________

nikos101 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:52 PM.


Advertisement
Log in to turn off these ads.