22,288
社区成员
以下是我使用cocos creator开发小游戏时,加载在常驻节点上的脚本。我想在其中实现微信小游戏手机本地存储游戏进度的功能,需要存储的变量分别是score和cardNum,经上传体验版本并真机测试,发现这个功能不能实现。有哪位可以帮忙看看问题出在哪吗?如能解决可加微信有偿。
import { _decorator, Component, director, game, Node, sys } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('persistentScript')
export class persistentScript extends Component {
score:number = 0;
cardNum:number[] = [];
isFirstLoad:boolean = true;
ifExplanation:boolean = true;
onLoad() {
if (this.isFirstLoad){
director.addPersistRootNode(this.node);
this.getData();
this.isFirstLoad = false;
}
}
setData() {
if (sys.platform === sys.WECHAT_GAME) {
let progress = {
score: this.score,
cardNum: this.cardNum};
wx.setStorageSync('gameProgress',JSON.stringify(progress));
}
}
getData() {
if (sys.platform === sys.WECHAT_GAME) {
let progressStr = wx.getStorageSync('gameProgress');
if(progressStr) {
let progress = JSON.parse(progressStr);
this.score = progress.score;
this.cardNum = progress.cardNum;
}
}
}
increaseScore(value:number){
this.score += value;
}
addcardNum(element:number){
this.cardNum.push(element);
}
getScore():number{
return this.score;
}
getCardNum():number[]{
return this.cardNum;
}
onDestroy() {
this.setData();
}
}