Jump to content

Bullets not dying.


wesleyorama
 Share

Recommended Posts

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>
Link to comment
Share on other sites

You need to kill the correct bullet, remove line 241 and kill the bullet there.

function p1Damage(player, bullet){    //destroyBullet()    bullet.kill(); //<--- killing the currect bullet    player2 +=1    p2ScoreText.text = 'Player2: ' + player2;}

If you want to call the destroyBullet() method, you need to send the bullet you want to kill, otherwise you will get the global reference and you will kill the wrong bullet.

destroyBullet(bullet);

Regards

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...