Hello, my name is Ivan and I’m new to phaser and game development.
I wanted to make a simple game where there’s a stack of blocks (color randomly generated) in the middle and 2 buttons corresponding to the blocks. If the button color clicked is the same with the block on the bottom of the stack, it’ll delete the block, otherwise, it’s a game over. The problem I’m encountering is that I can’t seem to make the block stack (or collide between each block). Here’s the look when I spawned the blocks.
And here’s the look when the blocks are finished moving.
And here’s my code for this game.
let sceneGame = new Phaser.Scene('Game');
sceneGame.preload = function()
{
this.load.path = ‘assets/’;
this.load.image(‘blockA’, ‘blockA.png’);
this.load.image(‘blockB’, ‘blockB.png’);
this.load.image(‘ground’, ‘floor.png’);
}
var buttonA;
var buttonB;
var totems;
var definer;
var grounds;
sceneGame.create = function()
{
buttonA = this.add.image(450, 580, ‘blockA’).setScale(2);
buttonB = this.add.image(50, 580, ‘blockB’).setScale(2);
buttonA.setInteractive();
buttonB.setInteractive();
totems = [];
definer = [];
grounds = [];
for(var i=0 ;i<7; i++){
var totemRandomizer = Math.floor(Math.random()*2)+1;
if(totemRandomizer == 1){
var totem = this.physics.add.sprite(250,50+i*-100,'blockA').setScale(2);
}
else{
var totem = this.physics.add.sprite(250,50+i*-100,'blockB').setScale(2);
}
definer.push(totemRandomizer);
totem.body.gravity.y = 500;
totems.push(totem);
for(var j=i-1;j>=0;j--){
this.physics.add.collider(totem,totems[j]);
}
}
for(var i=0;i<5;i++){
var ground = this.physics.add.sprite(50+i*100,700,'ground').setScale(2);
ground.setImmovable(true);
ground.body.allowGravity = false;
grounds.push(ground);
for(var j=0;j<totems.length;j++){
this.physics.add.collider(ground,totems[j]);
}
}
//red button
buttonA.on('pointerdown', function(){
if(definer[0] == 1){
totems[0].destroy();
totems.shift();
definer.shift();
console.log(totems.length);
}
else{
// console.log("game over");
sceneGame.scene.start('Result');
}
});
//green button
buttonB.on('pointerdown', function(){
if(definer[0] == 2){
totems[0].destroy();
totems.shift();
definer.shift();
console.log(totems.length);
}
else{
// console.log("game over");
sceneGame.scene.start('Result');
}
});
}
sceneGame.update = function(time, delta)
{
if(totems.length < 4){
var totemRandomizer = Math.floor(Math.random()*2)+1;
if(totemRandomizer == 1){
var totem = this.physics.add.sprite(250,-5850,‘blockA’).setScale(2);
}
else{
var totem = this.physics.add.sprite(250,-5850,‘blockB’).setScale(2);
}
definer.push(totemRandomizer);
totem.body.gravity.y = 500;
for(var j=i-1;j>=0;j--){
this.physics.add.collider(totem,totems[j]);
}
this.physics.add.collider(grounds[2],totems[j]);
}
}
Thank you for your time reading and replying to my question.