Greetings all,
I’m creating a game that is similar to Tetris in the sense that the blocks fall from the top portion of the screen and the player has control of each falling block until another block spawns.
I’ve created a function that loops through all created blocks(sprites) and I call the collision method on each newly created block against all previous blocks. For some reason this code only works on three blocks.
Subsequently it ceases to work and the blocks proceed to overlap.
Below is my code
const GAME_W=800;
const GAME_H=600;
let index = 0;
let grav = 1500;
//names of blocks
let block_names = ['dark_blue','green','light_blue','magenta','orange','pink','purple','red',
'seablue','white','yellow'];
let all_blocks=[];
let all_block_index =0;
let generate_ran = 0;
let mygroup;
let config = {type:Phaser.AUTO,
width:GAME_W,
height:GAME_H,
physics:{
default:'arcade',
arcade:{
gravity:{y :grav},
debug :false
}
},
scene: {
preload:preload,
create:create,
update:update
}
};
let game = new Phaser.Game(config);
function preload(){
this.load.image('sky','images/sky1.png');
for (let i =0; i <block_names.length; i++){
this.load.spritesheet(block_names[i],'images/blocks/Asset_'+block_names[i] + ".png",
{frameWidth:400,frameHeight:400});
}
}
function create(){
this.add.image(400,300,'sky');
//Timed generate block event
timedEvent = this.time.addEvent({ delay: 6000, callback: generate_blocks, callbackScope: this, loop: true });
}
function update(){
}
function generate_blocks(){
//Recycle through colored block array if at end of array
index = index >= block_names.length ? 0:index =index;
//Add new block
all_blocks[all_block_index] = this.physics.add.sprite(400,0,block_names[index]);
for (let i =0;i <all_blocks.length; i++){
//Add collider for current created block and previous
this.physics.add.collider(all_blocks[all_block_index],all_blocks[i]);
}
//scale block
all_blocks[all_block_index].scaleX=0.3;
all_blocks[all_block_index].scaleY=0.3;
all_blocks[all_block_index].setCollideWorldBounds(true);
//color index
all_blocks[all_block_index].setVisible(true);
index++;
all_block_index++;
generate_ran++;
}
Thanks for your time, any help is deeply appreciated.
indent preformatted text by 4 spaces