今回の追加内容
・文字をランダムな位置(上下)で発生させる
・ランダムに選ばれたアルファベットを表示今回のプログラムの変更と追加範囲
if (event.keyCode === Keyboard.KEY_CODE[mojiLabel.text]) {
var oraLabel = Label({
text: 'オラ!',
fill: 'black',
fontWeight: 'bold',
fontSize: 20,
}).addChildTo(this)
.setPosition(mojiLabel.x, mojiLabel.y);
oraLabel.tweener
.wait(100)
.call(function () {
oraLabel.remove();
});
mojiLabel.remove();
this.mojiList.shift();
}
});
},
update: function () {
var mojiKouho = 'abcdefghijklnmopqrstuvwsyz';
var mondaiMoji = mojiKouho.charAt(Random.randint(0, mojiKouho.length - 1));
if (Math.randbool(5)) {
var mojiLabel = Label({
text: mondaiMoji,
fill: 'black',
fontSize: 30,
}).addChildTo(this)
.setPosition(-50, Random.randint(100, this.height - 100));
▷文字をランダムな位置(上下)で発生させる
.setPosition(-50, Random.randint(100, this.height - 100));
randintはphina.jsで使えるメソッドで、最小値から最大値の間で乱数を生成して返す関数(Randomのメソッド)
setPositionはMainSceneの座標(0,0)を基準にして、x座標は-50(表示画面外左方向)、y座標は+100(下方向)~this.height(画面一番下)-100(上方向)の範囲を指定している。
▷ランダムに選ばれたアルファベットを表示
var mojiKouho = 'abcdefghijklnmopqrstuvwsyz'; var mondaiMoji = mojiKouho.charAt(Random.randint(0, mojiKouho.length - 1));
charAtは文字列の指定した位置にある文字を1つ返す関数
ここではmojiKouhoの文字列(a~z)からrandintでランダムに1文字選び、mondaiMojiに代入している。
var mojiLabel = Label({
text: mondaiMoji,
textをmondaiMojiにすることでランダムに選ばれたアルファベットを表示するようになる。