I’ve written this code so far and it’s all working fine. I have a sprite that I can move and there are falling objects to collect (eggs) and falling objects to avoid (comets) with a score board. However, no matter what I’ve tried, when the player overlaps the eggs or comets nothing at all happens. I really can’t figure out why this is and I’m very new to this so any help is appreciated. This is my code:
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: ‘arcade’,
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var player;
var eggs;
var comets;
var cursors;
var score = 0;
var gameOver = false;
var scoreText;
var game = new Phaser.Game(config);
function preload ()
{
this.load.image(‘sky’, ‘assets/sky.png’);
this.load.image(‘blue’, ‘assets/blue.png’);
this.load.image(‘comet’, ‘assets/comet.png’);
this.load.spritesheet(‘chick’, ‘assets/chick.png’, { frameWidth: 96, frameHeight: 96 });
}
function create ()
{
// sets the background for the game
this.add.image(400, 300, ‘sky’);
// adds in the player and their settings
player = this.physics.add.sprite(300, 450, 'chick');
player.setCollideWorldBounds(false); //can fall out of the world
// input events
cursors = this.input.keyboard.createCursorKeys();
eggs = this.physics.add.group()
comets = this.physics.add.group()
// the score is shown in the top left corner
scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#FFFFFF' });
//the eggs dropping on a timer
this.time.addEvent({
delay: 2000,
callback: dropEgg,
callbackScope: this,
loop: true
});
//the comets dropping on a timer
this.time.addEvent({
delay: 4000,
callback: dropComet,
callbackScope: this,
loop: true
});
// if the player overlaps with any eggs, the collectegg function is called
this.physics.add.overlap(player,eggs, collectEgg, null, this);
//if the player overlaps with any comets, the hitcomet function is called
this.physics.add.collider(player, comets, hitComet, null, this);
}
function update ()
{
if (gameOver)
{
return;
}
//allows the player to move with keys
if (cursors.left.isDown)
{
player.setVelocityX(-160);
}
else if (cursors.right.isDown)
{
player.setVelocityX(160);
}
else if (cursors.up.isDown)
{
player.setVelocityY(-120);
}
else{
player.setVelocityX(0)
}
}
//this is what will happen if an egg is collected
function collectEgg (player, eggs )
{
eggs.disableBody(true, true);
// Add and update the score
score += 10;
scoreText.setText('Score: ' + score);
}
//the function to drop eggs
function dropEgg ()
{
eggs = this.physics.add.group({
runChildUpdate: true
});
eggs.createMultiple({
key: 'blue',
repeat: 0, //one egg will drop at all time
});
eggs.children.iterate((child) => {
let x = Phaser.Math.Between(0, 800) //this will randomise where it is dropped
child.setX(x)
child.setY(-20)
child.setMaxVelocity(200) //the eggs have a set speed
})
}
//a function to drop comets
function dropComet ()
{
comets = this.physics.add.group({
runChildUpdate: true
});
comets.createMultiple({
key: 'comet',
repeat: 0, //one comet will drop at all time
});
comets.children.iterate((child) => {
let x = Phaser.Math.Between(0, 800) //this will randomise where it is dropped
child.setX(x)
child.setY(-20)
child.setMaxVelocity(200) //the comets have a set speed
})
}
//this is what will happen if a comet is hit
function hitComet (player, comets)
{
this.physics.pause();
player.setTint(0xff0000);
gameOver = true;
}