Where is “sprite” defined?

2019-03-04 09:13发布

问题:

Where exactly in this fiddle: (http://jsfiddle.net/wXs9J/) is "sprite" defined? I'm using the same type of animation in my own program but I get the line: "Uncaught TypeError: Cannot read property 'stateAnimations' of undefined" at the lines:

function drawSprite(sprite) {
    ctx.drawImage(
    sprite.stateAnimations[sprite.currentState].tileset.image, 
    sprite.stateAnimations[sprite.currentState].frames[sprite.stateAnimations[sprite.currentState].currentFrame].split(',')[0] * sprite.stateAnimations[sprite.currentState].tileset.tileWidth,
    sprite.stateAnimations[sprite.currentState].frames[sprite.stateAnimations[sprite.currentState].currentFrame].split(',')[1] * sprite.stateAnimations[sprite.currentState].tileset.tileHeight,
    sprite.stateAnimations[sprite.currentState].tileset.tileWidth,
    sprite.stateAnimations[sprite.currentState].tileset.tileHeight,
    Math.round(sprite.positionX),
    Math.round(sprite.positionY),
    sprite.width,
    sprite.height
);
}

Thank you in advance

回答1:

The object

var player = new Sprite({
    'left': spriteLeftAnim,
    'right': spriteRightAnim
}, 'right', canvas.width / 2, canvas.height / 2, 44, 108, 100);

is passed in as a parameter named sprite in the method render()

player is defined in a global scope in the same file.



回答2:

The Sprite consructor is on line 28 A sprite (called player) is created on line 72 This sprite is passed to drawSprite on line 107