Hi,
How can I do the collission between a group of bullets with a group of rocks (pedras). How can I do this?
var config = {
type: Phaser.WEBGL,
backgroundColor: '#FFFFFF',
width: 910,
height: 513,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var lastFired = 0;
var time;
var game = new Phaser.Game(config);
function preload ()
{
this.load.image('binho-nave', 'assets/binho-nave.png');
this.load.image('botao1', 'assets/botao1.png');
this.load.image('bullet', 'assets/bullet.png');
this.load.image('pedra', 'assets/pedra.png');
}
function create ()
{
// The player and its settings
player = this.physics.add.sprite(100, 450, 'binho-nave');
// Player physics properties. Give the little guy a slight bounce.
player.setBounce(0.2);
player.setCollideWorldBounds(true);
player.body.setGravityY(300);
// Botão Levantar a Nave
botao1 = this.add.image(76, 424, 'botao1');
botao1.setInteractive();
botao1.on('pointerdown', function (pointer) {
_voar();
});
// Botão atirar
botao2 = this.add.image(845, 424, 'botao1');
botao2.setInteractive();
botao2.on('pointerdown', function (pointer) {
_atirar();
});
// Pedra
pedras = this.physics.add.staticGroup({immovable: true});
pedras.create(250,500,'pedra');
// Bala
var Bullet = new Phaser.Class({
Extends: Phaser.GameObjects.Sprite,
initialize:
function Bullet (scene)
{
Phaser.GameObjects.Sprite.call(this, scene, 0, 0, 'bullet');
this.speed = Phaser.Math.GetSpeed(400, 1);
this.enableBody = true;
console.log(this);
},
fire: function (x, y)
{
this.setPosition(x+50, y);
this.setActive(true);
this.setVisible(true);
},
preUpdate: function (time, delta)
{
this.x += this.speed * delta;
if (this.x > 500)
{
this.setActive(false);
this.setVisible(false);
}
}
});
bullets = this.add.group({
classType: Bullet,
maxSize: 1,
enableBody: true,
runChildUpdate: true
});
cursors = this.input.keyboard.createCursorKeys();
this.physics.add.collider([bullets], [pedras],_checatiro,null, this);
}
function update ()
{
if (cursors.down.isDown)
{
_atirar();
}
if (cursors.up.isDown)
{
_voar();
}
}
function _voar() {
player.setVelocityY(-330);
}
function _atirar() {
var bullet = bullets.get();
if (bullet)
{
bullet.fire(player.x, player.y);
lastFired = time + 50;
}
}
function _checatiro(num1,num2){
console.log('a bala acertou a pedra');
//pedras.killAndHide(num1);
//pedras.killAndHide(num2);
}