Chapter 38

ゲーム画面の背景色を変える

alkn203
alkn203
2021.09.18に更新

backgroundcolor

画面の背景色を変える

画面の背景色を変える方法について説明します。

backgroundColorプロパティ

// 背景色
var colors = ['skyblue', 'black', 'gray', 'green', 'blue', 'red'];
// thisを退避
var self = this;
// タッチイベント    
this.onpointend = function() {
  // 背景色変更
  self.backgroundColor = colors.random();
};
  • 画面の背景色を変えるには、シーンのbackgroundColorの値を変更します。
  • 上の例では画面をタッチするとランダムに背景色が変わります。

サンプルコード

コードを見る
// グローバルに展開
phina.globalize();
/*
 * メインシーン
 */
phina.define("MainScene", {
  // 継承
  superClass: 'DisplayScene',
  // コンストラクタ
  init: function() {
    // 親クラス初期化
    this.superInit();
    // 背景色
    var colors = ['skyblue', 'black', 'gray', 'green', 'blue', 'red'];
    // thisを退避
    var self = this;
    // タッチイベント    
    this.onpointend = function() {
      // 背景色変更
      self.backgroundColor = colors.random();
    };
  },
});
/*
 * メイン処理
 */
phina.main(function() {
  // アプリケーションを生成
  var app = GameApp({
    title: '背景色を変える',
    // MainScene から開始
    //startLabel: 'main',
  });
  // fps表示
  //app.enableStats();
  // 実行
  app.run();
});

runstantプロジェクト

https://runstant.com/alkn203/projects/4f77264d