改造のヒント
class Enemy extends Actor {
*main() {
this.p=$pat_neko+44;
this.cy=this.y;//追加:往復する中心の座標
while(this.x>0) {
this.x+=this.vx;
this.y=this.cy+Math.sin($t)*10;//変更:三角関数で往復させる
if (this.rnd(100)<1) {
new EnemyBullet({x:this.x, y:this.y});
}
yield;
}
this.die();
}
}
class Enemy extends Actor {
*main() {
this.p=$pat_neko+44;//★この部分を変更
class Enemy extends Actor {
*main() {
this.n=this.rnd(2);// 0または1がthis.nに入る
if (this.n==0) this.p=$pat_neko+44;// 0だったら
if (this.n==1) this.p=$pat_neko+45;// 1だったら
class Player extends Actor {
*main() {
this.p=$pat_neko+45;
this.count=0;
while (true) {
this.x=$mouseX;
this.y=$mouseY;
if (this.count==30) {
new PlayerBullet({x:this.x, y:this.y, vx:10,vy:3});//変更
//★
this.count=0;
}
this.count+=1;
if (this.crashTo(Enemy) || this.crashTo(EnemyBullet)) {
break;
}
yield;
}
}
}
class PlayerBullet extends Actor {
*main() {
this.p=$pat_neko+48;
this.vx=10;
while (this.x<400) {
this.x+=this.vx;
this.y+=this.vy;//追加
var c=this.crashTo(Enemy)
if (c) {
$score++;
c.die();
break;
}
yield;
}
this.die();
}
}
class EnemyBullet extends Actor {
*main() {
this.p=$pat_neko+49;
// -- ここから追加
if (this.y>$player.y) {
this.vy=-1;
} else {
this.vy=1;
}
// -- ここまで追加
while(this.x>0) {
this.x-=10;
this.y+=this.vy;//追加
yield;
}
this.die();
}
}
プレイヤーが敵に触れたとき「ゲームオーバー」というメッセージを 表示し,スペースキーが押されたか画面をタッチされたらもう一度ゲームを始める.
※「タイトル画面」を作っている人は,ゲームオーバー後にタイトル画面に移動する方法を考えてみよう
class Player extends Actor {
*main() {
this.p=$pat_neko+45;
this.count=0;
while (true) {
this.x=$mouseX;
this.y=$mouseY;
if (this.count==30) {
new PlayerBullet({x:this.x, y:this.y});
this.count=0;
}
this.count+=1;
if (this.crashTo(Enemy) || this.crashTo(EnemyBullet)) {
break;//ゲームオーバーになると★に移動
}
yield;
}
//-----★ここを追加
new Label({x:100,y:100,text:"ゲームオーバー"});
this.p=-1;// キャラクタを消す
while (true) {
if(this.getkey("space")==1 || $touches[0].touched==1){
this.loadPage(Start);
}
yield;
}
//--ここまで
//this.die();
}
}
新しく Titleというクラスを作成し、次のように書く
class Title extends Actor {
*main() {
new Actor({x:$screenWidth/2,y:$screenHeight/2,text:"ゲームのタイトル"});
while(true){
if(this.getkey("space")==1 || $touches[0].touched==1){
this.loadPage(Start);
}
yield;
}
}
}
さらに、一番最後の行の
setMain(Start);
を
setMain(Title);
に変更する。