2015-11-05 52 views
0

我对javaScript和一般程序设计颇为陌生,我试图用Phaser API制作一对一的2D坦克游戏。Phaser P2物理。如何在与其他碰撞组发生冲突时如何杀死一个子弹(精灵)

我被困在过去的两天,试图找出如何杀死一个击中其他坦克的子弹。我确实设法使用街机物理和通过使用Phaser示例坦克游戏来工作。但我似乎无法将我的知识转换到目前为止,并将其应用于我目前正在使用的P2物理并希望坚持。

这是坦克构造函数,我用它来创建两个坦克,每个坦克持有其独立的子弹组命名为子弹,在最远的底部我有一个称为射击的功能,它重置子弹并使其飞向目标(这特定的功能主要是从移相器罐示例截取)

var tank = function(playerIndex, startX, startY, facing, keyLeft, keyRight, keyUp, keyDown, keyTLeft, keyTRight, keyShoot) { 
 
    this.playerIndex = playerIndex.toString();; 
 
    this.tankBody; 
 
    this.tankTurret; 
 
    this.facing = facing; 
 

 
    this.bullets; 
 
    this.fireRate = 200; 
 
    this.nextFire = 0; 
 

 
    this.health = 100; 
 
    this.isAlive = true; 
 

 
    this.bodyTurnSpeed = 2; 
 
    this.turretTurnSpeed = 2; 
 
    this.currentSpeed = 0; 
 
    this.maxSpeed = 50; 
 

 

 

 
    this.keyLeft = keyLeft; 
 
    this.keyRight = keyRight; 
 
    this.keyUp = keyUp; 
 
    this.keyDown = keyDown; 
 

 
    this.keyTLeft = keyTLeft; 
 
    this.keyTRight = keyTRight; 
 

 
    this.keyShoot = keyShoot; 
 

 
    this.create = function() { 
 
    if (this.playerIndex === "1") { 
 
     this.tankBody = game.add.sprite(startX, startY, "body_player_one"); 
 
     this.tankTurret = game.add.sprite(startX, startY, "turret_player_one"); 
 
    } else if (this.playerIndex === "2") { 
 
     this.tankBody = game.add.sprite(startX, startY, "body_player_two"); 
 
     this.tankTurret = game.add.sprite(startX, startY, "turret_player_two"); 
 
    } 
 

 
    this.tankBody.anchor.setTo(0.5, 0.5); 
 
    this.tankTurret.anchor.setTo(0.5, 0.5); 
 

 
    game.physics.p2.enable([this.tankBody]); 
 

 
    this.tankBody.body.immovable = false; 
 
    this.tankBody.body.collideWorldBounds = true; 
 
    this.tankBody.body.debug = false; 
 
    this.tankBody.body.fixedRotation = true; 
 
    this.tankBody.body.mass = 50; 
 
    // this.tankBody.body.kinematic = true; 
 

 
    this.bullets = game.add.group(); 
 
    this.bullets.enableBody = true; 
 
    this.bullets.physicsBodyType = Phaser.Physics.P2JS; 
 
    this.bullets.createMultiple(100, 'bullet', 0, false); 
 
    this.bullets.setAll('anchor.x', 0.5); 
 
    this.bullets.setAll('anchor.y', 0.5); 
 
    this.bullets.setAll('outOfBoundsKill', true); 
 
    this.bullets.setAll('checkWorldBounds', true); 
 

 
    switch (this.facing) { 
 
     case "left": 
 
     this.tankBody.rotation = this.tankBody.body.rotation = Phaser.Math.degToRad(-90); 
 
     this.tankTurret.rotation = Phaser.Math.degToRad(-90); 
 
     break; 
 
     case "right": 
 
     this.tankBody.rotation = this.tankBody.body.rotation = Phaser.Math.degToRad(90); 
 
     this.tankTurret.rotation = Phaser.Math.degToRad(90); 
 
     break; 
 
     case "up": 
 
     this.tankBody.rotation = this.tankBody.body.rotation = Phaser.Math.degToRad(0); 
 
     this.tankTurret.rotation = Phaser.Math.degToRad(0); 
 
     break; 
 
     case "down": 
 
     this.tankBody.rotation = this.tankBody.body.rotation = Phaser.Math.degToRad(180); 
 
     this.tankTurret.rotation = Phaser.Math.degToRad(180); 
 
     break; 
 
    } 
 
    } 
 

 
    this.update = function() { 
 
    if (this.isAlive) { 
 
     if (game.input.keyboard.isDown(this.keyLeft)) { 
 
     this.tankBody.rotation = this.tankBody.body.rotation -= Phaser.Math.degToRad(this.bodyTurnSpeed); 
 
     } 
 
     if (game.input.keyboard.isDown(this.keyRight)) { 
 
     this.tankBody.rotation = this.tankBody.body.rotation += Phaser.Math.degToRad(this.bodyTurnSpeed);; 
 
     } 
 

 
     if (game.input.keyboard.isDown(this.keyUp)) { 
 
     this.tankBody.body.moveForward(50); 
 
     } else if (game.input.keyboard.isDown(this.keyDown)) { 
 
     this.tankBody.body.moveBackward(50); 
 
     } else this.tankBody.body.setZeroVelocity(); 
 

 
     if (game.input.keyboard.isDown(this.keyTLeft)) { 
 
     this.tankTurret.rotation -= Phaser.Math.degToRad(this.turretTurnSpeed); 
 
     } else if (game.input.keyboard.isDown(this.keyTRight)) { 
 
     this.tankTurret.rotation += Phaser.Math.degToRad(this.turretTurnSpeed); 
 
     } 
 

 
     if (game.input.keyboard.isDown(this.keyShoot)) { 
 
     this.shoot(); 
 
     } 
 

 
     this.tankTurret.x = this.tankBody.x; 
 
     this.tankTurret.y = this.tankBody.y; 
 
    } else { 
 
     this.tankTurret.kill(); 
 
     this.tankBody.kill(); 
 
    } 
 
    } 
 

 
    this.shoot = function() { 
 
    if (game.time.now > this.nextFire && this.bullets.countDead() > 0) { 
 
     this.nextFire = game.time.now + this.fireRate; 
 
     var bullet = this.bullets.getFirstExists(false); 
 
     bullet.reset(this.tankTurret.x + this.tankTurret.width/2 * Math.cos(this.tankTurret.rotation - Phaser.Math.degToRad(90)), 
 
     this.tankTurret.y + this.tankTurret.width/2 * Math.sin(this.tankTurret.rotation - Phaser.Math.degToRad(90))); 
 
     bullet.body.rotation = this.tankTurret.rotation; 
 
     bullet.body.mass = 100; 
 
     bullet.body.moveForward(500); 
 

 
    } 
 
    } 
 
}

这是其中i分配collisionGroups并使其与海誓山盟碰撞, 这里的一切都如预期运作,但子弹没有dissapear

function create() { 
 
    game.add.sprite(0, 0, "background_one"); 
 

 
    game.physics.startSystem(Phaser.Physics.P2JS); 
 
    game.physics.p2.setImpactEvents(true); 
 

 
    //creating the collisiongroups 
 
    var bulletsCollisionGroup = game.physics.p2.createCollisionGroup(); 
 
    var playerOneCollisionGroup = game.physics.p2.createCollisionGroup(); 
 
    var playerTwoCollisionGroup = game.physics.p2.createCollisionGroup(); 
 
    var wallCollisionGroup = game.physics.p2.createCollisionGroup(); 
 

 
    //sets the objects to collide with gamestage borders (prevent objects from moving out of bounds) 
 
    game.physics.p2.updateBoundsCollisionGroup(); 
 

 
    //creating players, each player holds its own bulletgroup 
 
    player_one.create(); 
 
    player_two.create(); 
 

 
    //creates the tiles (mouseclick to place) 
 
    createTiles(); 
 

 
    //sets sprites to different collisiongroups 
 
    player_one.tankBody.body.setCollisionGroup(playerOneCollisionGroup); 
 
    for (var i = 0; i < player_one.bullets.children.length; i++) //player_one bullets 
 
    { 
 
    player_one.bullets.children[i].body.setCollisionGroup(bulletsCollisionGroup); 
 
    } 
 

 
    player_two.tankBody.body.setCollisionGroup(playerTwoCollisionGroup); 
 
    for (var i = 0; i < player_two.bullets.children.length; i++) //player_two bullets 
 
    { 
 
    player_two.bullets.children[i].body.setCollisionGroup(bulletsCollisionGroup); 
 
    } 
 

 
    for (var i = 0; i < tiles.children.length; i++) //tiles 
 
    { 
 
    tiles.children[i].body.setCollisionGroup(wallCollisionGroup); 
 
    } 
 

 

 
    //makes the collisiongroups collide with eachother 
 
    player_one.tankBody.body.collides([playerTwoCollisionGroup, wallCollisionGroup, bulletsCollisionGroup]); 
 
    player_two.tankBody.body.collides([playerOneCollisionGroup, wallCollisionGroup, bulletsCollisionGroup]); 
 

 
    for (var i = 0; i < tiles.children.length; i++) //tiles with everything 
 
    { 
 
    tiles.children[i].body.collides([playerOneCollisionGroup, playerTwoCollisionGroup, bulletsCollisionGroup]); 
 
    } 
 

 
    for (var i = 0; i < player_one.bullets.children.length; i++) //player_one bullets with everything 
 
    { 
 
    player_one.bullets.children[i].body.collides([wallCollisionGroup]); 
 
    player_one.bullets.children[i].body.collides(playerTwoCollisionGroup, function() { 
 
     bulletHitPlayer(player_two) 
 
    }, this); 
 
    } 
 

 
    for (var i = 0; i < player_two.bullets.children.length; i++) //player_two bullets with everything 
 
    { 
 
    player_two.bullets.children[i].body.collides([wallCollisionGroup]); 
 
    player_two.bullets.children[i].body.collides(playerOneCollisionGroup, function() { 
 
     bulletHitPlayer(player_one) 
 
    }, this); 
 
    } 
 
}

这是我试图使用与油箱碰撞回调函数,它似乎在街机物理与重叠工作

function bulletHitPlayerOne(tank, bullet) { 
 
    bullet.kill() 
 
    tank.health -= 20; 
 
    if (player.health <= 0) { 
 
    tank.isAlive = false; 
 
    } 
 

 
}

这就是我如何努力实现上面我collisionHandler功能

for (var i = 0; i < player_two.bullets.children.length; i++) { 
 
    player_two.bullets.children[i].body.collides(playerOneCollisionGroup, bulletHitPlayerOne, this); 
 

 
}

现在,我已经尝试了各种不同的方法来解决这个问题,但即时通讯完全被卡住,我开始认为我不能杀死P2组物理中的精灵(但为什么它不能工作?)

我做了seacrh并试图尽可能多地阅读文档,但是使用这个特别的问题,我似乎一个人:)

谢谢你的时间!

/Martin

+0

这里是目前形式的游戏 [坦克](http://madebysweden.net/games/tanks/main。html) –

+0

sprite.kill()应该可以工作 –

+0

Sprite.kill()应该可以工作,是的。但我的问题是如何在p2物理组中实现它。在我的例子中,我使用了kill命令 –

回答

0

这样的事应该工作。

Game.prototype = { 
 
    create: function(){ 
 
    //... 
 
    var bulletsCollisionGroup = game.physics.p2.createCollisionGroup(); 
 
    var playerOneCollisionGroup = game.physics.p2.createCollisionGroup(); 
 
    //.... 
 
    this.bullets = game.add.group(); 
 
    this.bullets.enableBody = true; 
 
    this.bullets.physicsBodyType = Phaser.Physics.P2JS; 
 
    this.bullets.createMultiple(100, 'bullet', 0, false); 
 
    this.bullets.setAll('anchor.x', 0.5); 
 
    this.bullets.setAll('anchor.y', 0.5); 
 
    this.bullets.setAll('outOfBoundsKill', true); 
 
    this.bullets.setAll('checkWorldBounds', true); 
 
    this.bullets.forEach(function(bullet){ 
 
     bullet.body.setCollisionGroup(bulletsCollisionGroup); 
 
     bullet.body.collides(playerOneCollisionGroup); 
 
    }); 
 
    
 
    player.body.setCollisionGroup(playerOneCollisionGroup); 
 
    player.body.collides(bulletsCollisionGroup, this.hit, this); 
 
    }, 
 
    /... 
 
    hit: function(player,bullet){ 
 
    bullet.parent.sprite.kill(); 
 
    } 
 
} 
 

记住,玩家将与子弹碰撞和子弹被杀害之前,它会改变速度,加速度等性能。你可能想要使用onBeginContact或者BroadphaseCallback

相关问题