Search the Community
Showing results for tags 'bullets'.
-
Hello! According to the examples I've created a tilemap from JSON file and added firing bullets with a left mouse clicking in a cursor's direction. Also a platform was added. My problem is that there are no collisions detected between a bullet and a tilemap's objects. But collisions works fine between the bullet and a platform. Here is my demo code: var config = { type: Phaser.CANVAS, width: 800, height: 600, backgroundColor: '#2d2d2d', parent: 'phaser-example', physics: { default: 'arcade', arcade: { gravity: { y: 300 }, debug: false } }, scene: { preload: preload, create: create } }; var game = new Phaser.Game(config); var bullets; var platforms; function preload () { this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/impact-tilemap.json'); this.load.image('kenney', 'assets/tilemaps/tiles/kenney.png'); this.load.image('ground', 'src/games/firstgame/assets/platform.png'); this.load.image('bullet', 'src/games/firstgame/assets/star.png'); } function create () { var map = this.make.tilemap({ key: 'map' }); var tileset = map.addTilesetImage('kenney'); var layer = map.createStaticLayer(0, tileset, 0, 0); layer.setCollisionByExclusion([-1]); this.physics.world.bounds.width = layer.width; this.physics.world.bounds.height = layer.height; platforms = this.physics.add.staticGroup(); platforms.create(400, 268, 'ground'); // Fires bullet on left click of mouse this.input.on('pointerdown', function() { fire(this); }, this); var Bullet = new Phaser.Class({ Extends: Phaser.GameObjects.Image, initialize: function Bullet(scene) { Phaser.GameObjects.Image.call(this, scene, 0, 0, 'bullet'); this.speed = Phaser.Math.GetSpeed(300, 1); this.velocity = new Phaser.Geom.Point(0, 0); }, fire: function (x, y, direction) { this.setPosition(x, y); this.setActive(true); this.setVisible(true); this.velocity.setTo(0, -this.speed); Phaser.Math.Rotate(this.velocity, direction); }, update: function (time, delta) { // Update position based on velocity this.x += this.velocity.x * delta; this.y += this.velocity.y * delta; } }); bullets = this.physics.add.group({ classType: Bullet, maxSize: 30, runChildUpdate: true }); this.physics.add.collider(bullets, platforms, callbackFunc, null, this); this.physics.add.collider(bullets, layer, callbackFunc, null, this); } function callbackFunc(bullet, target) { if ( bullet.active === true ) { console.log("Hit!"); bullet.setActive(false); bullet.setVisible(false); } } function fire(that) { var bullet = bullets.get(); if (bullet) { bullet.body.allowGravity = false; var angle = Math.atan2(that.input.activePointer.y - 400, that.input.activePointer.x - 300); bullet.fire(300, 400, angle + (3.14/2)); } } You can copy-paste it to any example at https://labs.phaser.io and start clicking to fire at any direction. I've tried to add a standard player from examples too and collisions work fine with the tilemap's objects. So the problem is with bullets only. Please help to solve this. Thanks!
-
- bullets
- collisions
-
(and 1 more)
Tagged with:
-
I have done a demo code that creates a gun (blue box) and bullets (red boxes). How can I set the initial position of the bullets depending of the rotation of the gun? Said with other words, if I rotate the gun (left/right arrows) , the bullets keep coming from the top side of the gun (box) instead of rotating accordingly. Code: http://plnkr.co/edit/3evg6J2cGo0nS7QKxiak?p=preview Preview: http://run.plnkr.co/AodG3n4Ykb3osdh2/
-
Hello, I created weapon animated from a spritesheet however the images on the sheet where a bit oversized. I know I can make the spritesheet smaller but it kindoff defeats my purpose of learning more in programming;) So when I scaled the weapon itself (fireball) to be half its sprites size its starting position also changed. Therefore I figured I' d anchor the weapon in the right position. But for some reason it keeps stating " anchor = undefined". So, anyone sees whats going wrong here? //This line is the one bugging out. I tried setting the anchor on each bullet and on the weapon itself. The console states 'anchor is undefined" this.weapon.anchor.setTo(0.5, 0.5); var weapon; create: function () { game.world.setBounds(0, 0, 1920, 560); game.physics.startSystem(Phaser.Physics.ARCADE); game.physics.arcade.gravity.y = 1000; this.weapon = game.add.weapon(1, "fireball"); // here I scale the weapon the fireball this.weapon.bullets.scale.setTo(0.5); this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; this.weapon.bulletSpeed = 300; this.weapon.fireRate = 500; //will use more sprites in the near future this.weapon.addBulletAnimation('fire',[23], 10, true ); this.weapon.trackSprite(player, 0, 0 , false); this.weapon.allowGravity = false; this.weapon.bulletGravity.y = -1000; //here I attempt to set the anchor so the weapon will be pushed to the right startposition this.weapon.anchor.setTo(0.5, 0.5); //this.weapon = action.attack.weapon; console.log(this.weapon); //u create controls cursors = game.input.keyboard.createCursorKeys(); //register hotkey this.fire = game.input.keyboard.addKey(Phaser.Keyboard.W); // Stop the following keys from propagating up to the browser game.input.keyboard.addKeyCapture([Phaser.Keyboard.E]); },
-
Hi there, I want to build a shotgun weapon for my game. This shotgun would shoot 3 (or more) bullets in a conical angle. However, from what I see, Phaser.Weapon only fires one bullet at a time. What do I do in order to get the weapon to fire 3 bullets consecutively
-
So I'm currently following @rich 's Shoot-em-up tutorial from over here https://phaser.io/tutorials/coding-tips-007. Please have a look at the source code if you haven't already. From what I understand he create's a weapon array that holds all the weapon types (each of which are an object inside a Weapon object). Now my question is How do I access the current weapon bullets in game.physics.arcade.collide? I've used game.physics.arcade.collide(this.weapon[this.currentWeapon], this.enemies) and this works fine, the bullets and the enemy collide well. BUT what if I want to kill that bullet? How would I pass the specific bullet that collided with the enemy into a function? This is what I've tried: game.physics.arcade.collide(this.weapon[this.currentWeapon], this.enemies, function(bullet, enemy) { bullet.kill(); }, null, this); However the above code doesn't work. Bullet is undefined. What do I do?
-
Hi there, I'm creating a simple platform game where you can shoot enemies. I've applied game.physics.arcade.gravity.y = 1000; But this seems to now affect my weapon class and bullets too! How do I set my weapons or bullets to NOT be affected by gravity? Here's the code I've used for reference: var playState = { //code to make assets goes here create: function(){ /*-- creating player --*/ game.add.sprite(0, 0, 'background'); game.physics.arcade.gravity.y = 1000; //Do not enable, need to fix gravity. this.player = game.add.sprite(game.width/2, 780, 'player'); this.player.anchor.setTo(0.5, 0.5); game.physics.arcade.enable(this.player); this.player.body.gravity.y = 1500; this.player.body.bounce.set(0.3); this.player.body.collideWorldBounds = true; this.player.cursor = game.input.keyboard.createCursorKeys(); this.player.fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); this.player.hp = 100; this.player.weapon = game.add.weapon(30, 'bullet'); // this.player.weapon.enableBody = true; this.player.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; this.player.weapon.bullets.setAll('body.gravity.y', 0); this.player.weapon.bulletSpeed = 850; this.player.weapon.fireRate = 200; this.player.weapon.trackSprite(this.player);
-
Hey everyone! So I'm pretty new to Phaser and I'm having a hard time making the player's bullets stop at a certain point (e.g killing the projectile at around 30px after its original position, etc). I want the player to have a limited projectile range and I'm just wondering how you can implement that because all my attempts of trying to figure it out have failed so far, so I'm seeking help from you guys... Thanks for any advise!
-
This seems like a simple z-order or positional issue. But I have a simple top down shooting example, and I want to have a player at the bottom which can rotate in place and shoot. Now overall the example is working. But the problem I'm having is the bullets are above the player sprite location. In my test case, I have boxes, and the bullets are originating from the center anchor point of my sprite. I'm trying to figure out how I can hide that by moving the player above the bullets, or if possible reposition the firing to the front of the player. My initial test was to adjust the Y, but that doesn't work when the sprite rotates... Is there some sort of local coordinate system I should use for position the origin of the shots? Here's my current example using Phaser Sandbox: http://phaser.io/sandbox/LsJAGKsr In the sandbox demo it's only slightly noticeable, my local test with boxes, it's more evident.
-
I want to scope after clicking the mouse flew in the direction of movement of the camera, thus emulated shot. Possible example on three.js: http://pauluskp.com/cannot My attempt to do so at Babylon: http://www.babylonjs-playground.com/#VWXHP#1 Here is the basic code, then I created a sphere when clicked, and then later in scene.registerBeforeRenderthe set coordinates camera adding a constant but probably incorrectly specified coordinates. window.addEventListener("click", function (e) { var bullet = new BABYLON.Mesh.CreateSphere('bullet', 3, 0.3, scene); var pos = camera.position; bullet.position = new BABYLON.Vector3( pos.x, pos.y, pos.z); bullet.material = new BABYLON.StandardMaterial('texture1', scene); bullet.material.diffuseColor = new BABYLON.Color3(3, 2, 0); var alpha = 0; scene.registerBeforeRender(function () { bullet.position = new BABYLON.Vector3( alpha*pos.x, alpha*pos.y, alpha*pos.z ); alpha += 0.01; });
- 5 replies
-
- Babylon.js
- bullets
-
(and 1 more)
Tagged with:
-
I know you guys have seen a million of these before. I have read a couple of responses to older posts, checked the examples, and found other demos where what I am trying to do is working. Basically what I am seeing is, if there is only one bullet on the screen then when that bullet hits the player on the right it dies. However if there is more than one bullet then the last bullet fired is killed and the others just pile up. You can see it in action here: http://thedatacloset.com/project/ WASD moves the player on the right, while SHIFT shoots. Arrow keys move the player on the left, while Space shoots. Only one player is currently coded to take "damage" and kill the bullet. That is the player to the left. My code is attached below. I have been banging my head against it for a while now, so the code is a little jumbled. <!doctype html><html> <head> <meta charset="UTF-8" /> <title>hello phaser!</title> <script src="js/phaser.min.js"></script> </head> <body> <script type="text/javascript"> window.onload = function() { var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() { game.load.image('sky', 'assets/sky.png'); game.load.image('ground', 'assets/platform.png'); game.load.image('bullet', 'assets/star.png'); game.load.spritesheet('dude', 'assets/dude.png', 32, 48);} var player;var platforms;var cursors;var mob;var facing;var mobFacing;var player1 = 0;var player2 = 0;var scoreText;var spaceKey;var bullet;var wKeyvar aKeyvar sKeyvar dKeyvar shiftKey function create() { // We're going to be using physics, so enable the Arcade Physics system game.physics.startSystem(Phaser.Physics.ARCADE); // A simple background for our game game.add.sprite(0, 0, 'sky'); // The platforms group contains the ground and the 2 ledges we can jump on platforms = game.add.group(); // We will enable physics for any object that is created in this group platforms.enableBody = true; // Here we create the ground. var ground = platforms.create(0, game.world.height - 64, 'ground'); // Scale it to fit the width of the game (the original sprite is 400x32 in size) ground.scale.setTo(2, 2); // This stops it from falling away when you jump on it ground.body.immovable = true; // Now let's create two ledges var ledge = platforms.create(400, 400, 'ground'); ledge.body.immovable = true; ledge = platforms.create(-150, 250, 'ground'); ledge.body.immovable = true; // The player and its settings player = game.add.sprite(32, game.world.height - 150, 'dude'); // We need to enable physics on the player game.physics.arcade.enable(player); // Player physics properties. Give the little guy a slight bounce. player.body.bounce.y = 0.2; player.body.gravity.y = 300; player.body.collideWorldBounds = true; // Our two animations, walking left and right. player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('right', [5, 6, 7, 8], 10, true); mob = game.add.sprite(750, 400, 'dude'); game.physics.arcade.enable(mob); mob.body.bounce.y = .02; mob.body.gravity.y = 300; mob.body.collideWorldBounds = true; mob.animations.add('left', [0,1,2,3], 10, true); mob.animations.add('right', [5,6,7,8], 10, true); // The score p1ScoreText = game.add.text(16, 16, 'Player1: 0', { fontSize: '32px', fill: '#000' }); p2ScoreText = game.add.text(600, 16, 'Player2: 0', { fontSize: '32px', fill: '#000' }); cursors = game.input.keyboard.createCursorKeys(); spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); wKey = game.input.keyboard.addKey(Phaser.Keyboard.W); aKey = game.input.keyboard.addKey(Phaser.Keyboard.A); sKey = game.input.keyboard.addKey(Phaser.Keyboard.S); dKey = game.input.keyboard.addKey(Phaser.Keyboard.D); shiftKey = game.input.keyboard.addKey(Phaser.Keyboard.SHIFT); bullets = game.add.group(); bullets.enableBody = true; bullets.physicsBodyType = Phaser.Physics.ARCADE; bullets.createMultiple(30, 'bullet', 0, false); bullets.setAll('anchor.x', 0.5); bullets.setAll('anchor.y', 0.5); bullets.setAll('outOfBoundsKill', true); bullets.setAll('checkWorldBounds', true); } function update() { // Collide the player and the stars with the platforms game.physics.arcade.collide(player, platforms); game.physics.arcade.collide(mob, platforms); game.physics.arcade.collide(player, mob); game.physics.arcade.collide(bullets, platforms); game.physics.arcade.collide(bullets, player, p1Damage, null, this); game.physics.arcade.collide(bullets, mob); // Reset the players velocity (movement) player.body.velocity.x = 0; mob.body.velocity.x = 0; if (cursors.left.isDown) { // Move to the left player.body.velocity.x = -150; player.animations.play('left'); facing = 'left'; } else if (cursors.right.isDown) { // Move to the right player.body.velocity.x = 150; player.animations.play('right'); facing = 'right'; } else { // Stand still player.animations.stop(); player.frame = 4; } // Allow the player to jump if they are touching the ground. if (cursors.up.isDown && player.body.touching.down) { player.body.velocity.y = -350; } if (aKey.isDown) { mob.body.velocity.x = -150; mob.animations.play('left'); mobFacing = 'left'; }else if (dKey.isDown) { mob.body.velocity.x = 150; mob.animations.play('right'); mobFacing = 'right'; }else{ mob.animations.stop(); mob.frame = 4; } if (wKey.isDown && mob.body.touching.down) { mob.body.velocity.y = -350; } if (shiftKey.isDown) { mobShoot(); } if (spaceKey.isDown) { shoot(); }}var shotTimer = 0;function shoot() { if (shotTimer <game.time.now) { shotTimer = game.time.now + 275; if (facing =='right') { bullet = bullets.create(player.body.x + player.body.width / 2 + 20, player.body.y + player.body.height / 2 - 4, 'bullet'); }else{ bullet = bullets.create(player.body.x + player.body.width / 2 - 20, player.body.y + player.body.height / 2 - 4, 'bullet'); } bullet.body.velocity.y = 0; if (facing == 'right') { bullet.body.velocity.x = 400; }else{ bullet.body.velocity.x = -400; } }}var mobShotTimer = 0;function mobShoot(){ if (mobShotTimer<game.time.now) { mobShotTimer = game.time.now + 275; bullet = bullets.getFirstExists(false); if (bullet) { if (mobFacing =='right') { bullet = bullets.create(mob.body.x + mob.body.width / 2 + 20, mob.body.y + mob.body.height / 2 - 4, 'bullet'); }else{ bullet = bullets.create(mob.body.x + mob.body.width / 2 - 20, mob.body.y + mob.body.height / 2 - 4, 'bullet'); } bullet.body.velocity.y = 0; if (mobFacing == 'right') { bullet.body.velocity.x = 400; }else{ bullet.body.velocity.x = -400; } } }}function destroyBullet(){ bullet.kill();}function p1Damage(player, bullet){ destroyBullet() player2 +=1 p2ScoreText.text = 'Player2: ' + player2;} }; </script> </body></html>
-
Hello! I've been playing around with the source code of the phaser example game called Tanks (http://examples.phaser.io/_site/view_full.html?d=games&f=tanks.js&t=tanks). However I have not figured a way to make the tanks target and shoot at the other tanks, so I would appreciate help. There is a line of code for the EnemyTank function to target the player: bullet.rotation = this.game.physics.arcade.moveToObject(bullet, this.player, 500); This makes the bullet fired by an EnemyTank function prototype go to towards the player's tank. I've been trying to figure out a way to modify the target (this.player) in the above line of code so that it would automatically fire at the closest other enemytank function, but I don't seem to get anywhere with it. The idea behind is to eventually make EnemyTank function prototypes (enemy tanks) fire bullets at nearby AllyTank prototypes (tanks allied with player).
-
How would one go about destroying a specific entity in an array? I have tried destroy, I have tried setting the object equal to null, but nothing has helped. After about 400 enemy ships spawn the game just lags. Any advice?
- 11 replies
-
- javascript
- bullets
-
(and 1 more)
Tagged with:
-
Hi. I'm trying to make my jet shoot red bullets, but for some reason they're coming out at green. Here's my code.... function Jet() { this.srcX = 0; this.srcY = 510; this.width = 100; this.height = 40; this.speed = 3; this.drawX = 220; this.drawY = 200; this.noseX = this.drawX + 100; this.noseY = this.drawY + 30; this.isUpKey = false; this.isRightKey = false; this.isDownKey = false; this.isLeftKey = false; this.isSpacebar = false; this.isShooting = false; this.bullets = []; this.currentBullet = 0; for (var i = 0; i < 25; i++) { this.bullets[this.bullets.length] = new Bullet(); }}Jet.prototype.draw = function() { clearCtxJet(); this.checkDirection(); this.noseX = this.drawX + 100; this.noseY = this.drawY + 30; this.checkShooting(); this.drawAllBullets(); ctxJet.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height);};Jet.prototype.checkDirection = function() { if (this.isUpKey) { this.drawY -= this.speed; } if (this.isRightKey) { this.drawX += this.speed; } if (this.isDownKey) { this.drawY += this.speed; } if (this.isLeftKey) { this.drawX -= this.speed; }};Jet.prototype.drawAllBullets = function() { for (var i = 0; i < this.bullets.length; i++) { if (this.bullets[i].drawX >= 0) this.bullets[i].draw(); if (this.bullets[i].explosion.hasHit) this.bullets[i].explosion.draw(); }};Jet.prototype.checkShooting = function() { if (this.isSpacebar && !this.isShooting) { this.isShooting = true; this.bullets[this.currentBullet].fire(this.noseX, this.noseY); this.currentBullet++; if (this.currentBullet >= this.bullets.length) this.currentBullet = 0; } else if (!this.isSpacebar) { this.isShooting = false; }};function clearCtxJet() { ctxJet.clearRect(0, 0, gameWidth, gameHeight);}// end of jet functions// bullet functionsfunction Bullet() { this.srcX = 100; this.srcY = 460; this.drawX = -20; this.drawY = 0; this.width = 5; this.height = 5; this.explosion = new Explosion();}Bullet.prototype.draw = function() { this.drawX += 3; ctxJet.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height); this.checkHitEnemy(); if (this.drawX > gameWidth) this.recycle();};Bullet.prototype.fire = function(startX, startY) { this.drawX = startX; this.drawY = startY;};Bullet.prototype.checkHitEnemy = function() { for (var i = 0; i < enemies.length; i++) { if (this.drawX >= enemies[i].drawX && this.drawX <= enemies[i].drawX + enemies[i].width && this.drawY >= enemies[i].drawY && this.drawY <= enemies[i].drawY + enemies[i].height) { this.explosion.drawX = enemies[i].drawX - (this.explosion.width / 2); this.explosion.drawY = enemies[i].drawY; this.explosion.hasHit = true; this.recycle(); enemies[i].recycleEnemy(); } }};Bullet.prototype.recycle = function() { this.drawX = -20;};// end of bullet functions// explosion functionsfunction Explosion() { this.srcX = 728; this.srcY = 520; this.drawX = 0; this.drawY = 0; this.width = 50; this.height = 50; this.hasHit = false; this.currentFrame = 0; this.totalFrames = 10;}Explosion.prototype.draw = function() { if (this.currentFrame <= this.totalFrames) { ctxJet.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height); this.currentFrame++; } else { this.hasHit = false; this.currentFrame = 0; }};// end of explosion functions// enemy functionsfunction Enemy() { this.srcX = 0; this.srcY = 559; this.width = 100; this.height = 40; this.speed = 2; this.drawX = Math.floor(Math.random() * 1000) + gameWidth; this.drawY = Math.floor(Math.random() * 360);}Enemy.prototype.draw = function() { this.drawX -= this.speed; ctxEnemy.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height); this.checkEscaped();};Enemy.prototype.checkEscaped = function() { if (this.drawX + this.width <= 0) { this.recycleEnemy(); }};Enemy.prototype.recycleEnemy = function() { this.drawX = Math.floor(Math.random() * 1000) + gameWidth; this.drawY = Math.floor(Math.random() * 360);};Any help would be gladly appreciated!!