0

I am following an on-line tutorial to create a simple game in HTML using canvas and JavaScript. I tried to load what seemed to be perfectly fine code but nothing is showing up on the screen and I received an error in the console saying

Uncaught SyntaxError: Unexpected token =

This is the portion of my code that seems to be incorrect:

player = {
        x: null,
        y: null,
        width: 20,
        height: 100,

        update = function() {},
        draw = function() {
            ctx.fillRect(this.x, this.y, this.width, this.height)
        };
    };

PS - this error is showing up in Chrome.

2 Answers2

1

When setting functions as properties of an object, you still need to declare them with :, not =.

You also don't need the ; after the function as that will give you an error once the : issue is fixed.

player = {
    x: null,
    y: null,
    width: 20,
    height: 100,
    update : function() {},
    draw : function() {
        ctx.fillRect(this.x, this.y, this.width, this.height)
    }
};
atmd
  • 7,430
  • 2
  • 33
  • 64
  • 1
    You also need to remove the semicolon from the draw function player = { x: null, y: null, width: 20, height: 100, update : function() {}, draw : function() { ctx.fillRect(this.x, this.y, this.width, this.height) } }; – Tommyka Apr 01 '15 at 08:40
1

You are using = signs in a literal object definition, just replace them with :

    update : function() {},
    draw : function() {
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }

As amtd specified in his answer, another error will show up after draw definition, because you are putting an extra ; illegal token inside a literal object definition

Community
  • 1
  • 1
axelduch
  • 10,769
  • 2
  • 31
  • 50