LoginSignup
0
0

More than 1 year has passed since last update.

【Phaser3】JSONファイルを読み込む

Posted at

はじめに

Phaser3にて、jsonファイルでゲームのデータを管理し、読み込むタイミングがあったが注意するべき点があったので、メモ。

内容

Phaser3ではシーンという概念が存在し、指定のタイミングで実行される関数が用意されている(preload(), create(), update())。
下記のように指定の関数内で処理を呼ぶことでjsonファイルを読み込み、ファイルの値を取得する。

jsonファイル読み込み
    preload() {
        // jsonファイルの読込
        this.load.json("key", "./assets/json/test.json");
    }
    
    create() {
        // jsonファイルの読込結果を変数に代入
        let jsonObj = this.cache.json.get("key");
    }

詳細

preload()は画面表示前、create()は画面生成時、update()は各関数が終了した後、1フレームごとに呼ばれる。
肝心なのは、preload()にてthis.load.json("key", [jsonファイルのパス])でファイルを読み込み、create()にてthis.cache.json.get("key")を実行しオブジェクトを取得する。

ファイルを読み込んですぐオブジェクトを取得しようとしても、エラーになってしまうので注意。

参考文献

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