Hi, I have been learning and recently I was trying to develop a game that uses the sprite (plane) to shoot some objects (stars), but have not been able to collide and destroy the object star, can anyone help me please?
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Shoot Plane</title>
<script src="//cdn.jsdelivr.net/npm/phaser@3.11.0/dist/phaser.js"></script>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
<script type="text/javascript">
var config = {
type: Phaser.AUTO, // supports Phaser.Canvas, Phaser.WebGl (auto = WebGl, if not supported will fall back to Canvas)
width: 800,
height: 600,
// add physics object
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false
}
},
scene: {
preload: preload, // calls the preload function
create: create, // create
update: update // update
}
};
var bullet;
var ship;
var stars;
var cursors;
var speed;
var stats;
var score = 0;
var lastFired = 0;
var scoreText;
var game = new Phaser.Game(config);
function preload () // loading anything needed to the scene
{
// storing images for the scene, giving it names to be called by other functions
this.load.image('sky', 'assets/sky.png');
this.load.image('ship', 'assets/ship.png');
this.load.image('bullet', 'assets/bullet.png');
this.load.image('star', 'assets/star.png');
}
// create the object by assigning width and height and the object need stored
// create methods for sprites to interact
function create ()
{
this.add.image(400, 300, 'sky');
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(400, 1);
},
fire: function (x, y)
{
this.setPosition(x, y - 50);
this.setActive(true);
this.setVisible(true);
},
update: function (time, delta)
{
this.y -= this.speed * delta;
if (this.y < -50)
{
this.setActive(false);
this.setVisible(false);
}
}
});
bullets = this.add.group({
classType: Bullet,
maxSize: 10,
runChildUpdate: true
});
stars = this.physics.add.group({
key: 'star',
repeat: 8,
setXY: ({x:100, y: 100, stepX:70})
});
ship = this.add.sprite(400, 500, 'ship').setDepth(1);
cursors = this.input.keyboard.createCursorKeys();
speed = Phaser.Math.GetSpeed(300, 1);
scoreText = this.add.text(16,16, 'Score: 0', {fontsize: '72px', fill: '#000'});
this.physics.add.collider(bullets, stars, hitTarget, null, this);
}
function hitTarget(bullet, star) {
star.disableBody(true, true);
}
function update(time, delta)
{
// set keys and their interations
if (cursors.left.isDown)
{
console.log("left is pressed");
ship.x -= speed * delta;
}
else if (cursors.right.isDown)
{
console.log("right is pressed");
ship.x += speed * delta;
}
if (cursors.up.isDown && time > lastFired)
{
console.log("up is pressed, fire!");
var bullet = bullets.get();
if (bullet)
{
bullet.fire(ship.x, ship.y);
lastFired = time + 100;
}
}
}
function hitTarget(player, star) {
// star to disappear
star.disableBody(true, true);
score+=10;
scoreText.setText('Score: ' + score);
if (stars.countActive(true) === 0) {
stars.children.iterate(function(child) {
child.enableBody(true, child.x, 0, true, true);
});
var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
var bomb = bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
}
}
</script>
</body>
</html>```