2015-11-05 54 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

相關問題