Sunday, 15 May 2011

javascript - this.image.width / number yields 0 -



javascript - this.image.width / number yields 0 -

i'm trying calculate width of sprite using image width, number comes out 0, why that?

function spritesheet(image, numframesx, numframesy, totalframes) { this.image = image; this.numframesx = numframesx; this.numframesy = numframesy; this.totalframes = totalframes; this.spritewidth = this.image.width / this.numframesx; this.spriteheight = this.image.height / this.numframesy; } image.onload = function() { console.log('image has been loaded'); } image.src = 'dance.png'; spritesheet = new spritesheet(image, 8, 10, 80);

spritesheet.spritewidth , spritesheet.spriteheight yields 0. cornered problem 'this.image.width' since works if set in width of image manually.

this.spritewidth = 880 / this.numframesx;

instead of

this.spritewidth = this.image.width / this.numframesx;

it works if calculate using object in console:

spritesheet.image.width / spritesheet.numframesx

yields 110

jsfiddle

how that?

window.onload = function () { console.log('image has been loaded'); image.src = 'dance.png'; spritesheet = new spritesheet(image, 8, 10, 80); }

javascript

No comments:

Post a Comment