Hello,
I am using react my code is below
import Phaser from 'phaser';
class MatchIT extends Phaser.Scene {
constructor() {
super({ key: 'MatchIT' });
}
preload() {
// Load the ball image
this.load.image('ball', '../assets/ball.jpeg');
}
create() {
// Create the ball sprite and place it in the center of the scene
// Create the ball sprite and place it in the center of the scene
this.ball = this.physics.add.sprite(this.cameras.main.width / 2, this.cameras.main.height / 2, 'ball');
this.ball.setOrigin(0.5, 0.5); // Set the origin to the center of the ball
this.ball.setCollideWorldBounds(true); // Make the ball collide with the world bounds
this.ball.setBounce(1); // Set the bounce factor
}
update() {
// Game logic here
}
// Expose a method to be called from React
customMethod() {
console.log('Custom method called from React!');
// Add your custom logic here
const originalY = this.ball.y;
this.tweens.add({
targets: this.ball,
y: originalY - 50,
duration: 250,
ease: 'Power1',
yoyo: true,
onComplete: () => {
this.ball.y = originalY; // Ensure the ball returns to its original position
}
});
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
scene: MatchIT,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false
}
},
};
const game = new Phaser.Game(config);
export const callCustomMethod = () => {
const scene =game.scene.keys['MatchIT'];
console.log(scene)
if (scene) {
scene.customMethod();
} else {
console.error('Scene not found or not initialized');
}
};
export default game;
There are no errors but when I run it i get a black screen and than nothing, I believe the scene is not loading correctly, how can I rectify the situation.
Thx