今回は敵の種類を増やしてみる。画像はEnemy1を使う。まずはobject/Enemy2.js。Eneym1がただ横に移動してただけに対して、斜め移動をするようにした。スコアも2倍にしておく
var Enemy2 = function(image){ this.position = new Point(); this.rect = new Rect(); this.life = 100; this.damage = 100; this.score = 2; this.moveUP = true; var fireCount = 0; this.draw = function(context){ context.drawImage(image, this.position.x, this.position.y); } this.doAction = function(info){ this.doMove(info); fireCount = (fireCount + 1) % 100; if(fireCount == 0){ var bullet = new EnemyBullet1(-4, 0); bullet.move(this.position.x + 2, this.position.y + 16); info.enemyBullets.push(bullet); } } this.doMove = function(info){ if(this.moveUP){ this.dmove(-2, -2); if(this.position.y < 20){ this.moveUP = false; } }else{ this.dmove(-2, 2); if(this.position.y > info.height - 20){ this.moveUP = true; } } } this.getRect = function(){ this.rect.left = this.position.x + 2; this.rect.top = this.position.y + 10; this.rect.right = this.position.x + 72; this.rect.bottom = this.position.y + 22; return this.rect; } } Enemy2.prototype = new GameObject();
次にHTMLでEnemy2.jsを読み込むようにする。
<script type="text/javascript" src="./object/Enemy2.js"></script>
次にevent/EventList.jsでEnemy2の出現タイミングと出現位置を定義しておく
var eventlist = [ {"name": "Enemy1", "frame": 0, "x": 650, "y": 100}, {"name": "Enemy1", "frame": 1, "x": 650, "y": 200}, {"name": "Enemy2", "frame": 100, "x": 650, "y": 200}, {"name": "Enemy2", "frame": 120, "x": 650, "y": 200} ]
最後にview/GameView.jsのcheckEvent。ここでEnemy2を出現するようにしておく
function checkEvent(){ if(eventlist[eventIndex] && eventlist[eventIndex].frame == frame){ var entry = eventlist[eventIndex]; if(entry.name == "Enemy1"){ var enemy = new Enemy1(images["Enemy1"]); enemy.move(entry.x, entry.y); gameInfo.enemies.push(enemy); }else if(entry.name == "Enemy2"){ var enemy = new Enemy2(images["Enemy1"]); enemy.move(entry.x, entry.y); gameInfo.enemies.push(enemy); } eventIndex++; } }