Jump to content

Adding function launchGreenEnemy() does not work


Recommended Posts

Hi, experts

I have try to combine two different tutorials into one game. Everything went well until I try to add function launchGreenEnemy(). I could not find out what went wrong, anyone has any suggestions ? 

I added the following code from http://codeperfectionist.com/articles/phaser-js-tutorial-building-a-polished-space-shooter-game-part-3/ combine with the Phaser Plug-in Joystick ! 

var greenEnemies;

            //  The baddies!
            greenEnemies = game.add.group();
            greenEnemies.enableBody = true;
            greenEnemies.physicsBodyType = Phaser.Physics.ARCADE;
            greenEnemies.createMultiple(5, 'enemy-green');
            greenEnemies.setAll('anchor.x', 0.5);
            greenEnemies.setAll('anchor.y', 0.5);
            greenEnemies.setAll('scale.x', 0.5);
            greenEnemies.setAll('scale.y', 0.5);
            greenEnemies.setAll('angle', 180);
            greenEnemies.setAll('outOfBoundsKill', true);
            greenEnemies.setAll('checkWorldBounds', true);


            // Enemy 
            function launchGreenEnemy() {
            var MIN_ENEMY_SPACING = 300;
            var MAX_ENEMY_SPACING = 3000;
            var ENEMY_SPEED = 300;

            var enemy = greenEnemies.getFirstExists(false);
            if (enemy) {
                enemy.reset(game.rnd.integerInRange(0, game.width), -20);
                enemy.body.velocity.x = game.rnd.integerInRange(-300, 300);
                enemy.body.velocity.y = ENEMY_SPEED;
                enemy.body.drag.x = 100;

            //  Send another enemy soon
            game.time.events.add(game.rnd.integerInRange(MIN_ENEMY_SPACING, MAX_ENEMY_SPACING), launchGreenEnemy);


Link to comment
Share on other sites

What is the exact problem? 

If the enemies don't show up its probably because the enemy is always reset whenever it is spawned. checkWorldBounds is true and it is spawned outside world bounds. In the tutorial the green ship sprite is large enough so when it is reset/spawned to y = -20 A very tiny bit is still inside the game world. try changing the y value to A lower number  (if you have small enemy sprites):

enemy.reset(game.rnd.integerInRange(0, game.width), -3);


Link to comment
Share on other sites


  • Recently Browsing   0 members

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