0

I am making a game in javascript, and I'm using making a class to hold all the sprites. However when I try to run it, safari gives me "SyntaxError: Unexpected use of reserved word 'class'". I can't find a reason it should be doing this. Below is my code:

var spritesContext;

//Sprite class
class sprite{
    constructor(imageName, imageX, imageY, imageHeight, imageWidth, context, canvas){
        this.imageName = imageName;
        this.imageX = imageX;
        this.imageY = imageY;
        this.imageHeight = imageHeight;
        this.imageWidth = imageWidth;
        this.canvas = canvas;
        this.context = context;
        spritesContext = context;
        console.log("Sprite constructed");
    }

    draw(){
        var character = new Image();
        character.src = "../Images/" + this.imageName + ".png";
        character.onload = function(){
            spritesContext.drawImage(character, this.imageX, this.imageY, this.imageWidth, this.imageHeight);
            console.log("Inner Draw is working.");
        }
        console.log("Draw is working.");
    }

    function getHeight(){
        return this.imageHeight;
    }

    function getWidth(){
         return this.imageWidth;
    }

    function getX(){
        return this.imageX;
    }

    function getY(){
       return this.imageY;
    }

    function moveUpX(e){
        this.imageX = (this.imageX + e);
    }

    function moveUpY(e){
        this.imageY = (this.imageY + e);
    }

    function moveBackX(e){
        this.imageX = (this.imageX - e);
    }

    function moveBackY(e){
       this.imageY = (this.imageY - e);
    }

    function changeImage(e){
        this.imageName = e;
    }

    function getImage(){
       return this.imageName;
    }

    function changeX(e){
         this.imageX = e;
    }

    function changeY(e){
        this.imageY = e;
    }

}
3
  • 1
    "class" is a new thing -- what version of javascript engine are you using? Commented Mar 1, 2016 at 2:33
  • It's a keyword in ES6. What browser/environment are you using this in? Commented Mar 1, 2016 at 2:35
  • You can't declare functions inside a class like that, that's an error Commented Mar 1, 2016 at 2:46

2 Answers 2

0

To make your code work, and more specifically the keyword class, you need to be in strict mode to enable certain browsers to interpret it.

To be in strict mode, add the following to the top of your file:

'use strict';
Sign up to request clarification or add additional context in comments.

3 Comments

To clarify a little: the language itself does not require strict mode to use these features, but the browsers that are rolling them out have enabled some in strict mode before standard mode.
Updated my answer based on your comment. Thanks.
And, at least Chrome is sensible enough to throw errors like ..class not yet supported outside strict mode, not Unexpected use...
0

enter image description here

Safari did not support "class" when the question was asked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.