TypeError: createjs.Sprite is not a constructor

8.2k views Asked by At

I am trying to make animation with an image file that is a SpriteSheet and when I run the code I get the error that is displayed in the title.

My code looks like this:

 Game.Hero = function (myX, myY) {
'use strict';
 var data = new createjs.SpriteSheet({
    "images":[Game.imgResSrcs["hero"]],
    "frames" : {
        "regX": myX, 
        "height": 60, 
        "regY": myY, 
        "width": 60
    },

    animations: {
        move: {
            frames: 
            [0,1,2,3,4,5,4,3,2,1],
            speed : 0.04
        },
        run: {
            frames: [1,2,3],
            speed:0.04
        }
    }
});
var startX = myX,
startY = myY,
speed = 2,
my = new createjs.Sprite(data, "move");

my.moveTo = function (newX, newY, tween) {
    var newXpx, newYpx;
    my.posX = newX;
    my.posY = newY;

    Game.stage.update();
};

I'm guessing it has something to do with this line: my = new createjs.Sprite(data, "move");

Am I forgetting something here or what could possibly be generating the error ?

2

There are 2 answers

1
Anto Jurković On BEST ANSWER

You are using:

my = new createjs.Sprite(data, "move");

and the error is: TypeError: createjs.Sprite is not a constructor

So, it seems you have to change that line to:

my = createjs.Sprite(data, "move");

But that is most probably not the correct because all example uses new createjs.Sprite().

Possible reason could be that you have no included Sprite in minified file.

0
tsveti_iko On

Try to do the following: 1. Include the CreateJS libraries in your project by linking to the CreateJS CDN inside the head tag:

<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>
  1. Create the new CreateJS instance after the page content (which includes the CreateJS libraries) is actually loaded:

    function init() {
        my = new createjs.Sprite(data, "move"); 
        // your code here
    };
    
    // load the create js function when page loaded
    if (document.readyState === "complete") {
        init();
    }
    else {
        window.onload = function () {
            init();
        };
    };