khleug35 Posted February 14, 2019 Share Posted February 14, 2019 Hello everyone , I would like to create save game function for my new game. so I using localStorage method, the following is my code, I write the script out of game.modules tab. if(localStorage.getItem("SAVEGAME") === undefined || localStorage.getItem("SAVEGAME") === null){ var savedata =0; localStorage.setItem("SAVEGAME", savedata); }else{ localStorage.getItem("SAVEGAME") } game.module( 'game.main' ) .require( 'game.player' ) .body(function() { game.createScene('Main', { init: function() { var text = new game.SystemText('Hello Panda'); text.size = 36; text.addTo(this.stage); } }); }); The game is not an error. but I Export project to build apk. the editor display this message. how to solve this problem? thank you very much. thx Quote Link to comment Share on other sites More sharing options...
enpu Posted February 16, 2019 Share Posted February 16, 2019 Hi! Can you send your log file, so i can take a look and see why the build is failing? Location of Panda 2 log files: macOS: ~/Library/Logs/Panda 2/log.log Windows: %USERPROFILE%\AppData\Roaming\Panda 2\log.log Quote Link to comment Share on other sites More sharing options...
khleug35 Posted February 16, 2019 Author Share Posted February 16, 2019 @enpu Thanks The following is my Panda 2 log files error code [2019-02-16 17:59:23.389] [error] panda-toolkit error: /Users/admin/Desktop/Game/Burning/src/game/main.js:2 if(localStorage.getItem("Upgrade") === undefined || localStorage.getItem("Upgrade") === null){ ^ ReferenceError: localStorage is not defined at Object.<anonymous> (/Users/admin/Desktop/Game/Burning/src/game/main.js:2:4) at Object.<anonymous> (/Users/admin/Desktop/Game/Burning/src/game/main.js:2757:3) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.require (module.js:498:17) at require (internal/module.js:20:19) at requireScriptTags (/Applications/Panda 2.app/Contents/Resources/app.asar/node_modules/panda-toolkit/build.js:82:13) [2019-02-16 17:59:23.404] [error] panda-toolkit exit: 1 the log display that localStorage is not defined. I think that the localStorage can't put out of game .modules tab. but the game can play on the web, only not work in mobile Any good idea to create the save game function in the game and work on mobile?? Thank you very much Quote Link to comment Share on other sites More sharing options...
khleug35 Posted February 17, 2019 Author Share Posted February 17, 2019 Oh dxmn !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I missed Storage Example from https://www.panda2.io/examples#storage-get In Panda2, in order to make save, I don't need to use localStorage it just use game.storage , my problem can solved. here is some example for saving game data game.config = { storage: { id: 'myGame' } }; game.module( 'game.main' ) .body(function() { game.createScene('Main', { init: function() { if(game.storage.get('score') === undefined || game.storage.get('score') === null ){ this.number = 1; game.storage.set('score', this.number); }else{ this.number = game.storage.get('score'); } this.shownumber = new game.SystemText(this.number); this.shownumber.size = 36; this.shownumber.font = 'serif'; this.shownumber.color = '#ff0000'; this.shownumber.align = 'center'; this.shownumber.x = game.width / 2; this.shownumber.addTo(this.stage); } , mousedown: function() { this.number++; game.storage.set('score', this.number); }, update: function() { this.shownumber.text = this.number; }, }); }); @enpu Thanks Quote Link to comment Share on other sites More sharing options...
Wolfsbane Posted February 18, 2019 Share Posted February 18, 2019 hmm. That's interesting. I would have thought the Panda game.storage would just use local storage. Quote Link to comment Share on other sites More sharing options...
enpu Posted February 20, 2019 Share Posted February 20, 2019 @Wolfsbane Storage class uses local storage. The reason why the build failed was that the code was placed outside of the module. // CODE OUTSIDE OF THE MODULE game.module( 'game.main' ) .body(function() { // CODE INSIDE THE MODULE }); The build process needs to go through the module file, so it can know which modules to include in the build (if module requires other modules). It uses Node and there is no local storage available, so that's why it throws "localStorage is not defined" error. Build process doesn't run the module's body function, so that's why you should put all the code there. khleug35 and Wolfsbane 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.