scion.miyazaki Posted September 9, 2014 Share Posted September 9, 2014 Hello, I have a little problem and I need your help.I am new in Phaser and I want to know How can I send a projectile to a target? In the Phaser's website there are many examples and it's very simple to test it. I want to send projectile to a target like this example : Shoot The Pointer (link) Me website test is : here (link) But I would like to add the gravity effect, like this : here (link) I don't know how to do that. Can you correct the code ?SCENE_WIDTH = 1024;SCENE_HEIGHT = 674;var game = new Phaser.Game(SCENE_WIDTH, SCENE_HEIGHT, Phaser.CANVAS, 'container', { preload: preload, create: create, update: update, render: render });function preload() { game.load.image('bullet', 'assets/gfx/bullet.png'); game.load.image('ground', 'assets/gfx/ground.png'); game.load.spritesheet('explosion', 'assets/gfx/explosion.png', 128, 128); game.load.image('a', 'assets/sprites/firstaid.png');};var SHOT_DELAY;var BULLET_SPEED;var NUMBER_OF_BULLETS;var GRAVITY;var gun;var bulletPool;var ground;var explosionGroup;var fpsText;var lastBulletShotAt;var target;function create() { // Set stage background color game.stage.backgroundColor = 0x4488cc; // Define constants SHOT_DELAY = 300; // milliseconds (10 bullets/3 seconds) BULLET_SPEED = 800; // pixels/second NUMBER_OF_BULLETS = 20; GRAVITY = 980; // 980 pixels/second/second target = game.add.sprite(700, 400, 'a'); // Create an object representing our gun gun = game.add.sprite(50, game.height - 64, 'bullet'); // Set the pivot point to the center of the gun gun.anchor.setTo(0.5, 0.5); // Create an object pool of bullets bulletPool = game.add.group(); for(var i = 0; i < NUMBER_OF_BULLETS; i++) { // Create each bullet and add it to the group. var bullet = game.add.sprite(0, 0, 'bullet'); bulletPool.add(bullet); // Set its pivot point to the center of the bullet bullet.anchor.setTo(0.5, 0.5); // Enable physics on the bullet game.physics.enable(bullet, Phaser.Physics.ARCADE); // Set its initial state to "dead". bullet.kill(); } // Turn on gravity game.physics.arcade.gravity.y = GRAVITY; // Create some ground ground = game.add.group(); for(var x = 0; x < game.width; x += 32) { // Add the ground blocks, enable physics on each, make them immovable var groundBlock = game.add.sprite(x, game.height - 32, 'ground'); game.physics.enable(groundBlock, Phaser.Physics.ARCADE); groundBlock.body.immovable = true; groundBlock.body.allowGravity = false; ground.add(groundBlock); } // Create a group for explosions explosionGroup = game.add.group(); // Simulate a pointer click/tap input at the center of the stage // when the example begins running. game.input.activePointer.x = game.width/2; game.input.activePointer.y = game.height/2 - 100; // Show FPS game.time.advancedTiming = true; fpsText = game.add.text( 20, 20, '', { font: '16px Arial', fill: '#ffffff' } );};function shootBullet() { // Enforce a short delay between shots by recording // the time that each bullet is shot and testing if // the amount of time since the last shot is more than // the required delay. if (lastBulletShotAt === undefined) lastBulletShotAt = 0; if (game.time.now - lastBulletShotAt < SHOT_DELAY) return; lastBulletShotAt = game.time.now; // Get a dead bullet from the pool var bullet = bulletPool.getFirstDead(); // If there aren't any bullets available then don't shoot if (bullet === null || bullet === undefined) return; // Revive the bullet // This makes the bullet "alive" bullet.revive(); // Bullets should kill themselves when they leave the world. // Phaser takes care of this for me by setting this flag // but you can do it yourself by killing the bullet if // its x,y coordinates are outside of the world. bullet.checkWorldBounds = true; bullet.outOfBoundsKill = true; // Set the bullet position to the gun position. bullet.reset(gun.x, gun.y); bullet.rotation = gun.rotation; // Shoot it in the right direction bullet.body.velocity.x = Math.cos(bullet.rotation) * BULLET_SPEED; bullet.body.velocity.y = Math.sin(bullet.rotation) * BULLET_SPEED;};function update() { if (game.time.fps !== 0) { fpsText.setText(game.time.fps + ' FPS'); } game.physics.arcade.collide(bulletPool, ground, function(bullet, ground) { // Create an explosion getExplosion(bullet.x, bullet.y); // Kill the bullet bullet.kill(); }, null, game); // Rotate all living bullets to match their trajectory bulletPool.forEachAlive(function(bullet) { bullet.rotation = Math.atan2(bullet.body.velocity.y, bullet.body.velocity.x); }, game); // Aim the gun at the pointer. // All this function does is calculate the angle using // Math.atan2(yPointer-yGun, xPointer-xGun) gun.rotation = game.physics.arcade.angleBetween(gun, target); //// gun.rotation = game.physics.arcade.angleToPointer(gun); // Shoot a bullet if (game.input.activePointer.isDown) { shootBullet(); }};// Try to get a used explosion from the explosionGroup.// If an explosion isn't available, create a new one and add it to the group.// Setup new explosions so that they animate and kill themselves when the// animation is complete.function getExplosion(x, y) { // Get the first dead explosion from the explosionGroup var explosion = explosionGroup.getFirstDead(); // If there aren't any available, create a new one if (explosion === null) { explosion = game.add.sprite(0, 0, 'explosion'); explosion.anchor.setTo(0.5, 0.5); // Add an animation for the explosion that kills the sprite when the // animation is complete var animation = explosion.animations.add('boom', [0,1,2,3], 60, false); animation.killOnComplete = true; // Add the explosion sprite to the group explosionGroup.add(explosion); } // Revive the explosion (set it's alive property to true) // You can also define a onRevived event handler in your explosion objects // to do stuff when they are revived. explosion.revive(); // Move the explosion to the given coordinates explosion.x = x; explosion.y = y; // Set rotation of the explosion at random for a little variety explosion.angle = game.rnd.integerInRange(0, 360); // Play the animation explosion.animations.play('boom'); // Return the explosion itself in case we want to do anything else with it return explosion;};function render () { // game.debug.text(game.time.physicsElapsed, 32, 32); // game.debug.body(player); //game.debug.bodyInfo(player, 16, 24);} Link to comment Share on other sites More sharing options...
JUL Posted September 9, 2014 Share Posted September 9, 2014 http://www.html5gamedevs.com/topic/5708-fire-bullet-to-predefined-angle/ Link to comment Share on other sites More sharing options...
Nepoxx Posted September 9, 2014 Share Posted September 9, 2014 I'm not going to "correct" your code unfortunately. However, here's a very useful link for you: http://gamemechanicexplorer.com/#bullets-4It seems to me that it is doing exactly what you want. Link to comment Share on other sites More sharing options...
Recommended Posts