I want to build a tower stacking game. A click triggers the block to fall down. I need to listen on when the falling stone goes to sleep and trigger an event afterwards to get the next round started.
To enable the sleep listeners I need to specify in the config matter: {enableSleeping: true}
.
But as soon as I set this my block doesn't fall any longer.
When I remove the line 63 block.setStatic(true);
, the block falls. But it falls without my interaction.
Also, if block.setStatic(true);
is set, the block goes to sleep. I need to prevent it from doing so, while I haven't interacted yet.
Does anybody know how I can fix this?
https://codepen.io/Wizardsmegid/pen/LYXKXEr
const sceneWidth = 380;
const sceneHeight = 600;
const stageOffset = 100;
const stageWidth = sceneWidth + stageOffset * 2;
const stageHeight = 2000;
const craneHeight = 100;
const craneDistance = 400;
const blockHeight = 100;
let craneSpeed = 1500;
const cameraPositionLeft = (stageWidth - sceneWidth) / 2;
const cameraPositionTop = stageHeight - sceneHeight;
let cranePositionTop = stageHeight - craneDistance;
let blockPositionTop = cranePositionTop;
let camera;
const config = {
type: Phaser.AUTO,
width: sceneWidth,
height: sceneHeight,
physics: {
default: 'matter',
matter: {
enableSleeping: true
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
const game = new Phaser.Game(config);
function preload() {
const background = this.load.image('sky', `https://fakeimg.pl/${stageWidth}x${stageHeight}/cfe1ff/`);
const crane = this.load.image('crane', `https://fakeimg.pl/100x${craneHeight}/6e6e6e/`);
const block = this.load.image('block', `https://fakeimg.pl/${blockHeight}x${blockHeight}/e7812f/`);
this.load.image('steadyBlock', `https://fakeimg.pl/100x50/e7812f/`);
}
const enableGravity = () => {
background.disableInteractive();
block.setStatic(false);
}
function create() {
camera = this.cameras.main;
background = this.add.image(0, 0, 'sky').setOrigin(0);
crane = this.add.image(300, cranePositionTop, 'crane').setOrigin(.5, 1);
steadyBlock = this.matter.add.image(stageWidth / 2, stageHeight, 'steadyBlock').setOrigin(.5, .5).setBounce(0).setMass(100);
camera.setBounds(cameraPositionLeft, 0, stageWidth, stageHeight);
camera.scrollY = cameraPositionTop;
this.matter.world.setBounds(0, 0, stageWidth, stageHeight);
const startRound = () => {
background.setInteractive();
block = this.matter.add.image(300, blockPositionTop, 'block').setOrigin(.5, .5).setBounce(0).setMass(10);
// block.setStatic(true);
block.setSleepEvents(true, true)
}
startRound();
background.on('pointerdown', enableGravity);
this.matter.world.on('sleepstart', (event, body) => {
console.log('block went to sleep');
})
}
function update() {
}