Phaser tank II

Preload

phaser.load.spritesheet('kaboom', 'img/explosion.png', 64, 64, 23);
phaser.load.atlas('enemy', 'img/enemy-tanks.png', 'assets/tanks.json')
this.enemies = [];
this.enemiesTotal = 20;
this.enemiesAlive = 20;

Initialize

this.ETbullets = [];
this.tank.health = 10;
this.tank.healthTotal = 10;
this.tank.alive = true;

Initialize

for (var i = 0; i < this.enemiesTotal; i++)

設定Enemy迴圈

var x = phaser.world.randomX;
var y = phaser.world.randomY;
var shadow = this.add.sprite(x, y, 'enemy', 'shadow');
var tank = this.add.sprite(x, y, 'enemy', 'tank');
var turret = this.add.sprite(x, y, 'enemy','turret');
var tank_group = phaser.add.group();

設定變數

tank_group.add(tank);
tank_group.add(turret);
tank_group.add(shadow);
tank.bringToTop();
turret.bringToTop();
shadow.anchor.setTo(0.5);
tank.anchor.setTo(0.5);
turret.anchor.setTo(0.3, 0.5);
                

Enemy setting

game.physics.enable(tank, Phaser.Physics.ARCADE);
tank.body.collideWorldBounds = true ;
tank.body.immovable = false ;
tank.body.bounce.setTo(1,1);
tank.angle = phaser.rnd.angle();

Add physics

this.enemies.push({
    tank: tank,
    turret: turret,
    tank_group: tank_group,
    hasbullet : false,
    shadow:shadow,
    nextFire: 0,
    health : 3,
    alive : true
})

Push value into array

this.explosions = game.add.group();
for (var i = 0; i < 10; i++)
{
    this.explosionAnimation = this.explosions.create
        (0, 0, 'kaboom', [0], false);
    this.explosionAnimation.anchor.setTo(0.5, 0.5);
    this.explosionAnimation.animations.add('kaboom');
}

Explosion effect

// tank angle change rate 
var tacr=0;
var bullets = this.bullets;
var enemies = this.enemies;
var explosionAnimation = this.explosionAnimation;
var explosions = this.explosions;
var ETbullets = this.ETbullets;

建立變數

game.physics.arcade.velocityFromRotation
    (enemies[i].tank.angle, 100, enemies[i].tank.body.velocity);
phaser.physics.arcade.collide(enemies[i].tank, this.tank);
var collided = phaser.physics.arcade.collide
    (enemies[i].tank, this.layer);

Collide

if(collided && tacr<=0){
    enemies[i].tank.angle += 4;
    tacr=5;
}

Collide with layer

if (enemies[i].tank.x >= phaser.world.width-32 || 
    enemies[i].tank.x <= 32 && tacr<=0){
        this.enemies[i].tank.angle += 4;
        tacr=15;
    }
if (enemies[i].tank.y >= phaser.world.height-32 || 
    enemies[i].tank.y <= 32 && tacr<=0){
            enemies[i].tank.angle += 4;
            tacr=15;
    }

Collide with world bounds

 if(enemies[i].alive)
{
    phaser.physics.arcade.overlap(bullets, enemies[i].tank, 
        function  (bullet , tank) {

       
    });

Bullet hit enemy

Text

bullet.kill();
enemies[i].health -=1;

if (enemies[i].health <=0)
{
    
}
  

Bullet hit enemy

Text

enemies[i].alive = false;
enemies[i].shadow.kill();
enemies[i].tank.kill();
enemies[i].turret.kill();
explosionAnimation = explosions.getFirstExists(false);
explosionAnimation.reset(enemies[i].tank.x, enemies[i].tank.y);
explosionAnimation.play('kaboom', 30, false, true);

Bullet hit enemy

Text

for(j = i+1;j < this.enemiesTotal ; j++)
{
    if(this.enemies[j].alive)
        {
            if (phaser.physics.arcade.collide
                (this.enemies[i].tank,this.enemies[j].tank))
                {
                    this.enemies[i].tank.angle += 4;
                    tacr=10;
                }
        }
}

Collide between tanks

this.enemies[i].tank_group.setAll('x',this.enemies[i].tank.x);
this.enemies[i].tank_group.setAll('y',this.enemies[i].tank.y);
this.enemies[i].tank_group.setAll('angle',this.enemies[i].tank.angle);
this.enemies[i].turret.rotation = this.game.physics.arcade.angleBetween
     (this.enemies[i].tank, this.tank);

Group

if (phaser.physics.arcade.distanceBetween(enemies[i].tank, tank) < 300)
 {
    if (phaser.time.now > this.nextFire && enemies[i].alive)
    {

    }
  }

Enemy fire setting

var ETbullet = phaser.add.sprite
    (enemies[i].turret.x, enemies[i].turret.y, 'bullet');
var ETbullets = this.ETbullets;
ETbullet.anchor.setTo(0.5, 0.5);
ETbullet.currentSpeed =300;
ETbullet.rotation = enemies[i].turret.rotation;
// bullet.scale.setTo(0.25,0.25);
  

Enemy fire setting

phaser.physics.enable(ETbullet);
phaser.physics.arcade.velocityFromRotation
    (ETbullet.rotation, ETbullet.currentSpeed, ETbullet.body.velocity);
ETbullet.outOfBoundsKill = true;
ETbullets.push(ETbullet);
this.nextFire = phaser.time.now + this.fireRate;

Enemy fire setting

phaser.physics.arcade.collide(ETbullets, this.layer, this.bulletHits);
phaser.physics.arcade.overlap(ETbullets, tank, function  (ETbullet , tank) {
    tank.health -= 1;
    ETbullet.kill();
    if(tank.health <=0)
    {
        shadow.kill();
        tank.kill();
        turret.kill();
    }
});
tacr-=1;

Enemy bullet physic

render: function () {

    var phaser = this.phaser
    
    phaser.debug.text('Health: ' + this.tank.health + ' / ' 
        + this.tank.healthTotal, 32, 32);

}

Health Point

this.shot =0;
this.cd =0;

Strike shoot

if (phaser.input.keyboard.isDown(Phaser.Keyboard.Q) && this.cd <= 0)
this.shot = 10;

Strike shoot

if(this.shot > 0)
    this.fire();
this.shot -=1;
this.cd -=1;

Strike shoot

fb.me/it.nchu

 nchuit.cc

來加入我們吧 yee~

 

phaser tank II

By Luke Chu

phaser tank II

  • 1,198