今回の追加内容
・無駄!追加
・オラ!と無駄!の処理の共通部分をremoveMoji(メソッド)にまとめる
phina.globalize();
phina.define('MainScene', {
superClass: 'DisplayScene',
init: function (option) {
this.superInit(option);
this.mojiList = [];
this.on('keydown', function (event) {
console.log(event);
var mojiLabel = this.mojiList[0];
if (mojiLabel == null) {
return;
}
if (event.keyCode === Keyboard.KEY_CODE[mojiLabel.text]) {
this.removeMoji(mojiLabel, {
text: 'オラ!',
fill: 'black',
fontWeight: 'bold',
fontSize: 30,
}, 100);
} else {
this.removeMoji(mojiLabel, {
text: '無駄!',
fill: 'red',
fontWeight: 'bold',
fontSize: 30,
}, 150);
};
});
},
update: function () {
//中略
removeMoji: function (mojiLabel, effect, effectTime) {
var effectLabel = Label(effect)
.addChildTo(this)
.setPosition(mojiLabel.x, mojiLabel.y);
effectLabel.tweener
.wait(effectTime)
.call(function () {
effectLabel.remove();
});
mojiLabel.remove();
this.mojiList.shift();
}
});
▷if文を使い無駄!を追加
if (event.keyCode === Keyboard.KEY_CODE[mojiLabel.text]) {
this.removeMoji(mojiLabel, {
text: 'オラ!',
fill: 'black',
fontWeight: 'bold',
fontSize: 30,
}, 100);
} else {
this.removeMoji(mojiLabel, {
text: '無駄!',
fill: 'red',
fontWeight: 'bold',
fontSize: 30,
}, 150);
};
▷removeMojiメソッド
removeMoji: function (mojiLabel, effect, effectTime) {
var effectLabel = Label(effect)
.addChildTo(this)
.setPosition(mojiLabel.x, mojiLabel.y);
effectLabel.tweener
.wait(effectTime)
.call(function () {
effectLabel.remove();
});
mojiLabel.remove();
this.mojiList.shift();
}
});
Label関数はLabelクラスのインスタンスを作る phina.js専用
removeメソッドはphina.jsで使えるメソッドで誰かの子供になっているときに自分を親から外す関数
shiftは配列の先頭の要素を削除して、残りを1つずつずらす関数
・オブジェクトのプロパティを参照する場合、.(ドット)を使う
mojiLabel.remove(); mojiLabelのremoveという名前のプロパティを参照
removeプロパティの値は関数なのでメソッド