Jump to content

Adding a working weapon to enemy group


LuizzAugusto
 Share

Recommended Posts

Hello! I'm creating a basic shoot em up with Phaser CE/v2, but I'm not capable to add a working weapon to the enemy group.

Here is the enemy group code:

function GroupFactory(image) {
    var group = game.add.group();
    var sprite = game.add.sprite(0, 0, image);
    
    group.dimension = {};
    group.dimension.width = sprite.width;
    group.dimension.height = sprite.height;
    sprite.kill();

    return group;
}

function Enemies(image) {
    var group = GroupFactory(image);
    group.speed = 4;
    group.enableBody = true;

    group.update = function(time, score) {
        if(time >= 2000) {
            var sprite = group.create(game.rnd.realInRange(0, game.world.width - group.dimension.width), -group.dimension.height, image);
            var limit = game.rnd.realInRange(2,4);

            sprite.life = 3;
            sprite.weapon = Weapon(1, 'bullet', sprite, 0, -sprite.width, 90, 400, 800);
            sprite.weapon.fire();
        
            sprite.collisionsDefaultAction = function() {
                game.physics.arcade.enable(sprite);
                
                if(sprite.life > 0) {
                    sprite.life--;
                    sprite.visible = false;
                }
                else {
                    sprite.kill();
                    score.value += 1; 
                }
            };

            time = 0;

            sprite.update = function() {
                if(sprite.y <= game.world.height/limit - sprite.height) {
                    sprite.y += group.speed;
                }

                if(!sprite.visible && sprite.life > 0)
                    setTimeout(function() {
                        sprite.visible = true;
                    }, 100);
            };
        }


        return time;
    };

    return group;
}

And weapon code:

function WeaponFactory(number, image, object, x, y) {
    var sprite = game.add.weapon(number, image);

    sprite.bullets.setAll('scale.x', scale);
    sprite.bullets.setAll('scale.y', scale);
    sprite.trackSprite(object, x, y);

    return sprite;
}

function Weapon(number, image, object, x, y, bulletAngleSet, fireRate, bulletSpeed) {
    var sprite = WeaponFactory(number, image, object, x, y);

    sprite.bulletKillType = Phaser.Weapon.KILL_DISTANCE;
    sprite.bulletKillDistance = game.world.height;

    sprite.bulletAngleSet = bulletAngleSet;
    sprite.fireRate = fireRate;
    sprite.bulletSpeed = bulletSpeed*scale;

    return sprite;
}

Update code:

function update() {
    if(this.player.visible) {
        this.text.visible = false;
        this.player.update();
        this.score.update();
        this.time = this.enemies.update(this.time, this.score);
        this.time += 20;

        this.enemies.forEach(function(enemy) {
            enemy.update();
        }, this);

        game.physics.arcade.overlap(this.player, this.enemies, collisionHandler, null, this);
        game.physics.arcade.overlap(this.player.weapon.bullets, this.enemies, collisionHandler, null, this);
    } else {
        this.gameOverText.visible = true;
        RetryTouch(this.player, this.enemies, this.score);
    }
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...