Heru Posted September 3, 2014 Share Posted September 3, 2014 Hi all, so, I wanted to learn Phaser for quite some time, now I finally gotten to it.A while ago, I created a Space Invaders type of game in regular javascript without a framework.For my first steps in Phaser, I thought it would be a good idea to try to recreate it. I have a scrolling background, a player ship, some enemies, and different weapons for the player.That's where my problem is: In the "Invaders" example, the player always fires just one bullet,I want him to fire more bullets at the same time, that come out of different cannons on the ship.The bullets and enemies are defined like this:var player_bullet = ['group', 'standard', 'double', 'trident', 'quad'];player_bullet['standard'] = new Object();player_bullet['standard']['group'];player_bullet['standard']['time'] = 200;player_bullet['standard']['speed'] = -100;player_bullet['standard']['name'] = 'Normal Fire';player_bullet['double'] = new Object();player_bullet['double']['group'];player_bullet['double']['time'] = 250;player_bullet['double']['speed'] = -95;player_bullet['double']['name'] = 'Double Fire';player_bullet['trident'] = new Object();player_bullet['trident']['group'];player_bullet['trident']['time'] = 350;player_bullet['trident']['speed'] = -90;player_bullet['trident']['name'] = 'Trident Fire';player_bullet['quad'] = new Object();player_bullet['quad']['group'];player_bullet['quad']['time'] = 400;player_bullet['quad']['speed'] = -90;player_bullet['quad']['name'] = 'Quad Fire';var player_bullet_time = 0;Going from the "Invaders" example, my create-phase includes this:/*the different weapons the player can shoot.*/player_bullet['standard']['group'] = this.game.add.group();player_bullet['standard']['group'].enableBody = true;player_bullet['standard']['group'].physicsBodyType = Phaser.Physics.ARCADE;player_bullet['standard']['group'].createMultiple(30, 'player_bullet_standard');player_bullet['standard']['group'].setAll('anchor.x', '0.5');player_bullet['standard']['group'].setAll('anchor.y', '1');player_bullet['standard']['group'].setAll('outOfBoundsKill', true);player_bullet['standard']['group'].setAll('checkWorldBounds', true); player_bullet['double']['group'] = this.game.add.group();player_bullet['double']['group'].enableBody = true;player_bullet['double']['group'].physicsBodyType = Phaser.Physics.ARCADE;player_bullet['double']['group'].createMultiple(30, 'player_bullet_standard');player_bullet['double']['group'].setAll('anchor.x', '0.5');player_bullet['double']['group'].setAll('anchor.y', '1');player_bullet['double']['group'].setAll('outOfBoundsKill', true);player_bullet['double']['group'].setAll('checkWorldBounds', true); player_bullet['trident']['group'] = this.game.add.group();player_bullet['trident']['group'].enableBody = true;player_bullet['trident']['group'].physicsBodyType = Phaser.Physics.ARCADE;player_bullet['trident']['group'].createMultiple(30, 'player_bullet_standard');player_bullet['trident']['group'].setAll('anchor.x', '0.5');player_bullet['trident']['group'].setAll('anchor.y', '1');player_bullet['trident']['group'].setAll('outOfBoundsKill', true);player_bullet['trident']['group'].setAll('checkWorldBounds', true); player_bullet['quad']['group'] = this.game.add.group();player_bullet['quad']['group'].enableBody = true;player_bullet['quad']['group'].physicsBodyType = Phaser.Physics.ARCADE;player_bullet['quad']['group'].createMultiple(30, 'player_bullet_standard');player_bullet['quad']['group'].setAll('anchor.x', '0.5');player_bullet['quad']['group'].setAll('anchor.y', '1');player_bullet['quad']['group'].setAll('outOfBoundsKill', true);player_bullet['quad']['group'].setAll('checkWorldBounds', true); player_bullet['group'] = this.game.add.group();player_bullet['group'].add(player_bullet['standard']['group']);player_bullet['group'].add(player_bullet['double']['group']);player_bullet['group'].add(player_bullet['trident']['group']);player_bullet['group'].add(player_bullet['quad']['group']);player_current_bullet = player_bullet['standard'];When a bullet is fired, it calls the firePlayerBullet() function:firePlayerBullet: function() { // To avoid them being allowed to fire too fast we set a time limit if (this.game.time.now > player_bullet_time) { var bullet = ['1', '2', '3', '4']; bullet['1'] = 0; bullet['2'] = 0; bullet['3'] = 0; bullet['4'] = 0; switch (player_current_bullet['name']) { case player_bullet['quad']['name']: bullet['4'] = player_bullet['quad']['group'].getFirstExists(false); //no break case player_bullet['trident']['name']: bullet['3'] = player_bullet['trident']['group'].getFirstExists(false); //no break case player_bullet['double']['name']: bullet['2'] = player_bullet['double']['group'].getFirstExists(false); //no break case player_bullet['standard']['name']: bullet['1'] = player_bullet['standard']['group'].getFirstExists(false); break; } if (bullet['1']) { switch(player_current_bullet['name']) { case player_bullet['quad']['name']: bullet['1'].reset(player_ship.x + 10, player_ship.y - 20);break; case player_bullet['trident']['name']: bullet['1'].reset(player_ship.x + 20, player_ship.y - 15);break; case player_bullet['double']['name']: bullet['1'].reset(player_ship.x + 10, player_ship.y - 20);break; case player_bullet['standard']['name']: bullet['1'].reset(player_ship.x, player_ship.y - 20);break; } bullet['1'].body.velocity.y = player_current_bullet['speed']; } if (bullet['2']) { switch(player_current_bullet['name']) { case player_bullet['quad']['name']: bullet['2'].reset(player_ship.x - 10, player_ship.y - 20);break; case player_bullet['trident']['name']: bullet['2'].reset(player_ship.x - 20, player_ship.y - 15);break; case player_bullet['double']['name']: bullet['2'].reset(player_ship.x - 10, player_ship.y - 20);break; } bullet['2'].body.velocity.y = player_current_bullet['speed']; } if (bullet['3']) { switch(player_current_bullet['name']) { case player_bullet['quad']['name']: bullet['3'].reset(player_ship.x + 20, player_ship.y - 15); bullet['3'].body.velocity.y = player_current_bullet['speed']; bullet['3'].body.velocity.x = -player_current_bullet['speed']/4; bullet['3'].angle = 15; break; case player_bullet['trident']['name']: bullet['3'].reset(player_ship.x, player_ship.y - 20); bullet['3'].body.velocity.y = player_current_bullet['speed']; bullet['3'].angle = 0; break; } } if (bullet['4']) { bullet['4'].reset(player_ship.x - 20, player_ship.y - 15); bullet['4'].body.velocity.y = player_current_bullet['speed']; bullet['4'].body.velocity.x = player_current_bullet['speed']/4; bullet['4'].angle = -15; } if (bullet['1'] || bullet['2'] || bullet['3'] || bullet['4']) { player_bullet_time = this.game.time.now + player_current_bullet['time']; } } }the collision looks like this:this.game.physics.arcade.overlap(player_bullet['group'], enemy_current['group'], this.collisionHandler, null, this);So, if anyone could tell me, how I can handle it, that the player can shoot 2 or 3 or 4 bullets at a time from different locations, I would be more than happy! I know, my code isn't the best one, and while coding it I thought "that can't be right" and "there's got to be a better way to do it" ... I hope you guys can help me out best regards Edit: formating Link to comment Share on other sites More sharing options...
Heru Posted September 5, 2014 Author Share Posted September 5, 2014 >130 views and no one could help? But, in the meantime, I figured it out myself. I can't fire four different bullets, I have to fire one bullet four times (at nearly the same time). the function for the bullets now looks like this:firePlayerBullet: function() { if (this.game.time.now > player_bullet_time) { var bullet; if (player_current_bullet['name'] == player_bullet['standard']['name']) { bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x, player_ship.y - 20); bullet.angle = 0; bullet.body.velocity.y = player_bullet['standard']['speed']; player_bullet_time = this.game.time.now + player_current_bullet['time']; } else if (player_current_bullet['name'] == player_bullet['double']['name']) { bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x + 10, player_ship.y - 20); bullet.angle = 0; bullet.body.velocity.y = player_bullet['double']['speed']; bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x - 10, player_ship.y - 20); bullet.angle = 0; bullet.body.velocity.y = player_bullet['double']['speed']; player_bullet_time = this.game.time.now + player_current_bullet['time']; } else if (player_current_bullet['name'] == player_bullet['trident']['name']) { bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x + 20, player_ship.y - 15); bullet.angle = 0; bullet.body.velocity.y = player_bullet['trident']['speed']; bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x - 20, player_ship.y - 15); bullet.angle = 0; bullet.body.velocity.y = player_bullet['trident']['speed']; bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x, player_ship.y - 20); bullet.angle = 0; bullet.body.velocity.y = player_bullet['trident']['speed']; player_bullet_time = this.game.time.now + player_current_bullet['time']; } else if (player_current_bullet['name'] == player_bullet['quad']['name']) { bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x + 10, player_ship.y - 20); bullet.angle = 0; bullet.body.velocity.y = player_bullet['quad']['speed']; bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x - 10, player_ship.y - 20); bullet.angle = 0; bullet.body.velocity.y = player_bullet['quad']['speed']; bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x + 20, player_ship.y - 15); bullet.angle = 15; bullet.body.velocity.y = player_bullet['quad']['speed']; bullet.body.velocity.x = -player_bullet['quad']['speed']/4; bullet = player_bullet['group'].getFirstExists(false); bullet.reset(player_ship.x - 20, player_ship.y - 15); bullet.angle = -15; bullet.body.velocity.y = player_bullet['quad']['speed']; bullet.body.velocity.x = player_bullet['quad']['speed']/4; player_bullet_time = this.game.time.now + player_current_bullet['time']; } } }Maybe that'll help someone in the future... Link to comment Share on other sites More sharing options...
Recommended Posts