suttyclem Posted March 14, 2016 Share Posted March 14, 2016 var game = new Phaser.Game(1100, 650, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() { game.load.image('background', './assets/images/space.jpg'); game.load.image('blob', './assets/images/spaceship1_final.jpg'); game.load.image('star', './assets/images/star.jpg'); game.load.image('asteroid1', './assets/images/asteroids.jpg'); game.load.image('asteroid2', './assets/images/Asteroids-icon.jpg'); game.load.image('ufo', './assets/images/UFO-icon.jpg'); game.load.image('start', './assets/images/diggonaut-start.png'); game.load.image('bullet', './assets/images/missile-alien.jpg'); }; // Creating sprites and groups var background; var blobSprite; var asteroids1; var asteroids2; var stars; // var bonusFood; // Bullets // var bullet; var bullets; var bulletTime = 0; // controlling game start var startButton; var playing = false; var fireRate = 100; var nextFire = 0; // To control movement of asteroids and stars var nextMovedStar; var nextMovedAsteroid1; var randomSelection; var interval = 3500/20; // CREATING SCORES var score = 0; var scoreText; function create() { // Canvas display game.renderer.clearBeforeRender = false; game.renderer.roundPixels = true; // arcade physics game.physics.startSystem(Phaser.Physics.ARCADE); // background game.add.tileSprite(0, 0, game.width, game.height, 'background'); // ============= PLAYER SPRITE =========== // blobSprite = game.add.sprite(300, 300, 'blob'); blobSprite.anchor.set(0.5); game.physics.enable(blobSprite, Phaser.Physics.ARCADE); // asteroid style movement blobSprite.body.drag.set(300); blobSprite.body.maxVelocity.set(600); // =========== ASTEROIDS ============= // Creating asteroids1 timer game.time.events.loop(Phaser.Timer.SECOND * 3, createAsteroids1, this); // Creating asteroids2 timer // game.time.events.loop(Phaser.Timer.SECOND * 5, createAsteroids2, this); // ============== BULLETS =============== bullets = game.add.group(); bullets.enableBody = true; bullets.physicsBodyType = Phaser.Physics.ARCADE; bullets.createMultiple(40, 'bullet'); bullets.setAll('anchor.x', 0.5); bullets.setAll('anchor.y', 0.5); // Enable keys to work cursors = game.input.keyboard.createCursorKeys(); game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR ]); }; function update() { // Checking for overlaps game.physics.arcade.overlap(blobSprite, stars, collectStar, null, this); game.physics.arcade.overlap(blobSprite, asteroids1, gameOver, null, this); game.physics.arcade.overlap(blobSprite, asteroids2, gameOver, null, this); // overlaps with bullets game.physics.arcade.overlap(bullets, asteroids1, destroyAsteroid1, null, this); game.physics.arcade.overlap(bullets, asteroids2, destroyAsteroid2, null, this); if (playing) { // Controlling movements if (cursors.up.isDown) { game.physics.arcade.accelerationFromRotation(blobSprite.rotation, 200, blobSprite.body.acceleration); } else { blobSprite.body.acceleration.set(0); } if (cursors.left.isDown) { blobSprite.body.angularVelocity = -300; } else if (cursors.right.isDown) { blobSprite.body.angularVelocity = 300; } else { blobSprite.body.angularVelocity = 0; } if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) { fireBullet(); } // Screen wrapping sprite and bullets screenWrap(blobSprite); bullets.forEachExists(screenWrap, this); }; }; function screenWrap(blobSprite) { if (blobSprite.x < 0) { blobSprite.x = game.width; } else if (blobSprite.x > game.width) { blobSprite.x = 0; } if (blobSprite.y < 0) { blobSprite.y = game.height; } else if (blobSprite.y > game.height) { blobSprite.y = 0; } } // Bullet firing function function fireBullet () { if (game.time.now > bulletTime) { var bullet = bullets.getFirstExists(false); if (bullet) { bullet.reset(blobSprite.body.x + 16, blobSprite.body.y + 16); bullet.lifespan = 2000; bullet.rotation = blobSprite.rotation; game.physics.arcade.velocityFromRotation(blobSprite.rotation, 400, bullet.body.velocity); bulletTime = game.time.now + 50; } } }; // creating the floating asteroids function createAsteroids1() { asteroids1 = game.add.group(); var asteroid1 = asteroids1.create(game.world.randomX, game.world.randomY, 'asteroid1'); // Make the little buggers move about game.time.events.loop(interval, function() { nextMovedAsteroid1 = game.rnd.integerInRange(0, asteroids1.length); this.game.add.tween(asteroids1.getAt(nextMovedAsteroid1)).to({x: this.game.world.randomX, y: this.game.world.randomY}, 19000, Phaser.Easing.Linear.InOut, true); }, this); game.physics.enable(asteroids1, Phaser.Physics.ARCADE); }; // Creating the shooting asteroids function createAsteroids2() { asteroids2 = game.add.group(); var asteroid2 = asteroids2.create(game.world.randomX, game.world.randomY, 'asteroid2'); game.physics.enable(asteroids2, Phaser.Physics.ARCADE); // This shoots the object at the blob asteroids2.forEachAlive(function(shoot) { game.physics.arcade.moveToObject(shoot, {x: blobSprite.x, y: blobSprite.y}, 200, this); }, this); }; function destroyAsteroid1(bullet, asteroid) { console.log('destroy function called'); asteroid.destroy(); }; function destroyAsteroid2(bullet, asteroid) { asteroid.destroy(); }; function gameOver() { console.log('game over'); alert('You lost, game over!'); location.reload(); playing = false; }; function startGame() { startButton.destroy(); playing = true; }; Hi all, i'm new to coding and this is my first phaser game so I'm happy to now be contributing to the community I'm working on a game that the player gets killed if he gets hit by an asteroid and can also shoot the asteroid. The game was working ok before I tried adding the shooting, but now that I have only some asteroids are registering as overlapping for bot the bullets and the player sprite. Can you have two overlap functions for one group? That's my best what the problem may be. Also, I'm not sure if my code snippet was satisfactory e.g. if there was things I didn't or should have included, so if you have any advice for that then please let me know. Thanks for the help! Link to comment Share on other sites More sharing options...
drhayes Posted March 14, 2016 Share Posted March 14, 2016 The first line in createAsteroids is "asteroids1 = game.add.group();" which replaces the old group; that means that any asteroids in that group aren't getting collided anymore. If the group already exists you don't need to re-create it. Try removing that line and creating the asteroid groups in your "create" function and see what happens. suttyclem 1 Link to comment Share on other sites More sharing options...
suttyclem Posted March 15, 2016 Author Share Posted March 15, 2016 @drhayes thanks a lot mate, that's worked like a charm! Link to comment Share on other sites More sharing options...
Recommended Posts