javascript - Save gameplay in localStorage -


i have this code:

storage.prototype.setobj = function(key, obj) {     return this.setitem(key, json.stringify(obj)) } storage.prototype.getobj = function(key) {     return json.parse(this.getitem(key)) } function onready() {     if (localstorage["piecesarray"]) {         piecesarray = new array();         piecesarray.length = 0;          piecesarray = localstorage.getobj("piecesarray");     } else {         piecesarray = new array();     }     can = document.getelementbyid('mycanvas');     ctx = can.getcontext('2d');              img = new image();     img.onload = onimage1load;     img.src = "http://www.niagaraparks.com/images/wallpaper/niagarafalls_640x480.jpg"; } 

and want save game state in localstorage, , after refreshing page want refresh game state.

what doing wrong?

so, there's 2 problems noticed in fiddle.

first, have function you're calling isn't defined, you're not running setobj function.

if (drawhighlight) highlightrect(drawx, drawy); // liczymy kliknięcia clickcounter(); // function doesn't exist localstorage.setobj("piecesarray", piecesarray); 

i added in dummy clickcounter function doesn't anything.

additionally, loading piecesarray local storage, overwriting anyway. once image loads, need check see if have piecesarray. did this:

function onimage1load() {     var r;     if (piecesarray.length === 0) {         (var = 0; < 4; i++) {             (var j = 0; j < 3; j++) {                 r = new rectangle(i * blocksize, j * blocksize, * blocksize + blocksize, j * blocksize + blocksize);                 piecesarray.push(r);             }         }          scramblearray(piecesarray, 30);     }     drawimage();  } 

working fiddle


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -