Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

About write code out of game.modules tab and build apk.


khleug35
 Share

Recommended Posts

Hello everyone:D ,  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.

aaa.png.c33e687ba0e73ca9043f6e19c7b4ba9f.png

 

how to solve this problem?  thank you very much.;) thx

 

 

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

Oh  dxmn !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  :ph34r:

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  :)

 

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...