マイペースなプログラミング日記

DTMやプログラミングにお熱なd-kamiがマイペースに書くブログ

簡単なシューティングゲーム作成 その7

前回までは敵が攻撃してこなかったので、今回は敵が攻撃するように変更した。まずは ./object/EnemyBullet1.js。コンストラクタで1フレームあたりに移動する距離を受け取り、doActionで移動する

var EnemyBullet1 = function(dx, dy){
    this.position = new Point();
    this.rect = new Rect();
    this.life = 10000;
    this.damage = 100;
    this.score = 100000;

    this.doAction = function(info){
        this.dmove(dx, dy);
    }

    this.draw = function(context){
        context.fillStyle = 'rgb(255, 0, 0)';
        context.beginPath();
        context.arc(this.position.x + 1, this.position.y + 1, 2, 0, 360, true);
        context.closePath();
        context.fill();
    }

    this.getRect = function(){
        this.rect.left = this.position.x;
        this.rect.top = this.position.y;
        this.rect.right = this.rect.left + 2;
        this.rect.bottom = this.rect.top + 2;
        
        return this.rect
    }
}

EnemyBullet1.prototype = new GameObject();

次に ./object/Enemy1.js。まずはEnemy1に変数を加える。これは敵が登場してからのフレーム数となる。これが一定の数になったら攻撃するようにする。

var fireCount = 0;

次にEnemy1のdoActionメソッド。弾を追加する処理を加えた。

this.doAction = function(info){
    this.dmove(-2, 0);
    fireCount = (fireCount + 1) % 200;
        
    if(fireCount == 0){
        var bullet = new EnemyBullet1(-4, 0);
        bullet.move(this.position.x + 2, this.position.y + 16);

        info.enemyBullets.push(bullet);
    }
}