LoginSignup
1
1

More than 5 years have passed since last update.

kiwi.js入門: 背景画像を表示、クリック回数を描画

Last updated at Posted at 2015-07-22

参考

必要なファイル

  • kiwi.js
  • bg.png : 適当な画像をご用意ください。
  • index.html : これから作ります。
  • gettingStarted.js : これから作ります。

出来上がるもの

  • 背景画像(bg.png)の表示
  • 背景画像をクリックしたらクリック回数が表示されます。

スクリーンショット 2015-07-22 21.02.23.png

作成

index.html
<meta charset=utf-8 />
<script type="text/javascript" src="kiwi.js"></script>
<script type="text/javascript" src="gettingStarted.js"></script>
gettingStarted.js
//描画サイズ
var gameOptions = {
    width: 768,
    height: 512
};

// stateをPlayにセット
var state = new Kiwi.State('Play');

//spaceにbg.pngを割当て。以降spaceという名前で参照できる
state.preload = function () {
    this.addImage('space', 'bg.png');
};

// 即時関数。実行内容を定義
state.create = function () {
    //背景の定義
    this.space = new Kiwi.GameObjects.Sprite(this, this.textures.space);
    //文字の定義
    this.textField = new Kiwi.GameObjects.Textfield(this, '');
    this.textField.x = this.game.stage.width / 2;
    this.textField.y = 10;
    this.textField.color = '#FFFFFF';
    this.textField.fontFamily = 'Roboto, sans-serif';
    this.textField.textAlign = Kiwi.GameObjects.Textfield.TEXT_ALIGN_CENTER;
    //文字用のカウンター。クリックした回数用
    this.counter = 0;
    //背景を有効にする
    this.addChild(this.space);
    //文字を有効にする
    this.addChild(this.textField);
    //イベントハンドラ(背景をクリックするとstate.clickedの処理を行う)
    this.space.input.onUp.add(this.clicked, this);
};

//背景をクリックするとstate.clickedの処理を行う
state.clicked = function(){
    this.counter++;
    this.textField.text = this.counter + ' 回クリックしました。';
};

var game = new Kiwi.Game('game-container', 'LoadingAnImage', state, gameOptions);

起動

python -m SimpleHTTPServer

今回使わなかったが、スプライト画像を頂いておく。

1
1
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
1
1