0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【25日目】25日間でCocos Creatorでゲームを作る

0
Posted at

ログインしてるユーザーを保持する

ローカルストレージにログインユーザーの情報を保持する

  1. PlayerData.tsを編集する
    PlayerData.ts
    import { sys } from 'cc';
    
    export class PlayerData {
      public static username: string = "";
      public static highScore: number = 0;
      public static level: number = 1;
    
      private static readonly SAVE_KEY = 'MY_GAME_PLAYER_DATA';
    
      public static save() {
        const data = {
          username: this.username,
          highScore: this.highScore,
          level: this.level
        };
        sys.localStorage.setItem(this.SAVE_KEY, JSON.stringify(data));
        console.log("PlayerData saved to storage.");
      }
    
      public static load() {
        const json = sys.localStorage.getItem(this.SAVE_KEY);
        if (json) {
          const data = JSON.parse(json);
          this.setPlayerData(data);
        }
      }
    
      public static setPlayerData(data: any) {
        this.username = data.username || "Guest";
        this.highScore = data.highScore || 0;
        this.level = data.level || 1;
        console.log(`PlayerDataにセット: ${this.username} (Best: ${this.highScore})`);
      }
    }
    
  2. MenuManager.tsでPlayerDataを読み込む処理を追記する
    MenuManager.ts
    onLoad() {
      PlayerData.load();
    }
    

まとめ

これで一度ログインした後は更新しても、ログインしたユーザー情報が保持されたままになりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?