Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

Trying to save and load the game state using javascript using localStorage.setItem but receiving a type error

Below is the class piece which we need to store in an object and save but when we try to do it using localstorage.setitem we are getting a type error.

class Piece {

constructor(type, game){

//convenience 
this.Game=game;

if (!type) type='';

type=type.toUpperCase();
this.name=type;

//every piece has its shape as a bitmap and other
//properties describing its presentation
switch (type){
    
    case 'F':this.trayPosition=0;
             this.bitMap=[
             [0,0,0,0,0],
             [0,0,1,1,0],
             [0,1,1,0,0],
             [0,0,1,0,0],
             [0,0,0,0,0]
            ]; break;

    case 'I':this.trayPosition=1;
             this.bitMap=[
             [0,0,1,0,0],
             [0,0,1,0,0],
             [0,0,1,0,0],
             [0,0,1,0,0],
             [0,0,1,0,0]
            ]; break;


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

localStorage.setItem() accepts key and value as arguments. Both arguments need to be in format of a string. Your best option is to use JSON.stringify on your value before storing it. Later you can load it from storage and parse it, using JSON.parse.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...