CharlesCraft50 Posted September 26, 2016 Share Posted September 26, 2016 I am making my own top view game and i just added a weapon, it is a pistol, I want to change the direction of the bullets by not using angleVelocity because if I use it, the pistol will rotate and i dont like it. i just want to change the direction of the bullets like if you click the left button, the pistol will change its direction to the left and also the bullet. Sample Code: function create() { //some update function code... player = game.add.sprite(100, 390, 'player'); weapon = game.add.weapon(ammo, 'bullet'); weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; weapon.bulletAngleOffset = 90; weapon.bulletSpeed = 600; weapon.fireRate = 500; //The pistol that follow the player pistol = game.add.sprite(player.body.x, player.body.y, 'Pistol'); game.physics.arcade.enable(pistol); pistol.scale.setTo(0.5, 0.5); weaponType = ""; weapon.trackSprite(pistol, 0, 0, true); fireButton = game.input.keyboard.addKey(Phaser.Keyboard.Q); } function update() { //some update function code... if (fireButton.isDown) { if(facing == "left") { weapon.fire(); //I want this to fire in the left direction } else if(facing == "right") { weapon.fire(); //I want this to fire in the right direction } else if(facing == "up") { weapon.fire(); //I want this to fire in the up direction } else if(facing == "down") { weapon.fire(); //I want this to fire in the down direction } } } Link to comment Share on other sites More sharing options...
PhasedEvolution Posted September 26, 2016 Share Posted September 26, 2016 You have to specify the direction the bullet is gonna fire as you wish. Something similar to: IF LEFT: bullet.x -= 100; IF RIGHT bullet.x += 100; IF UP bullet.y -= 100; IF DOWN bullet.y += 100; Probably on your weapon.fire function Link to comment Share on other sites More sharing options...
samme Posted September 26, 2016 Share Posted September 26, 2016 if (fireButton.isDown) { if (facing == "left") { weapon.fireAngle = Phaser.ANGLE_LEFT; } else if (facing == "right") { weapon.fireAngle = Phaser.ANGLE_RIGHT; } else if (facing == "up") { weapon.fireAngle = Phaser.ANGLE_UP; } else if (facing == "down") { weapon.fireAngle = Phaser.ANGLE_DOWN; } weapon.fire(); } Jiro101 1 Link to comment Share on other sites More sharing options...
CharlesCraft50 Posted September 27, 2016 Author Share Posted September 27, 2016 @samme I tried that one, they are not working, they will work if I remove weapon.trackSprite(pistol, 0, 0, true); in your code but the bullets didn't fire in the right place. Link to comment Share on other sites More sharing options...
CharlesCraft50 Posted September 27, 2016 Author Share Posted September 27, 2016 I already fixed it by changing the weapon.trackSprite(pistol, 0, 0, true); to weapon.trackSprite(pistol, 0, 0, false);, Thank you @samme for the code :)... Link to comment Share on other sites More sharing options...
CharlesCraft50 Posted September 27, 2016 Author Share Posted September 27, 2016 How to automatically remove the bullet when it hits in the enemy or in the other solid objects? I know this code will work but it will destroy all bullets and will not work the next time you fire: game.physics.arcade.collide(weapon.bullets, enemy, function(){weapon.bullets.destroy();}) Link to comment Share on other sites More sharing options...
samme Posted October 2, 2016 Share Posted October 2, 2016 On 9/27/2016 at 4:54 AM, CharlesCraft50 said: How to automatically remove the bullet when it hits in the enemy or in the other solid objects? game.physics.arcade.collide(enemy, weapon.bullets, function (enemy, bullet){ bullet.kill() }); quiphop 1 Link to comment Share on other sites More sharing options...
Recommended Posts