Hi all,
I’m creating a game with Phaser3 by extending the Scene and Sprite classes in separate modules file and get this error message after running it:
Uncaught TypeError: Cannot read properties of undefined (reading ‘isParent’)
at World.collideObjects (phaser.js:126291:21)
at Collider.update (phaser.js:129644:20)
at World.update (phaser.js:125511:30)
at EventEmitter.emit (phaser.js:1928:33)
at Systems.step (phaser.js:49066:16)
at SceneManager.update (phaser.js:100289:21)
at Game.step (phaser.js:162805:20)
at TimeStep.step (phaser.js:89366:14)
at step (phaser.js:89613:19)
I’ve tried searching for the solution for hours but still can’t figure it out. Here’s my JS file (i combined all the modules into one js file and stripped out the export and import statements):
//------------------------------MODULE: VARIABLES-----------------------------------
let w=640;
let h=480;
//------------------------------MODULE: BASE SCENE CLASS------------------------------------
class BaseScene extends Phaser.Scene{
constructor(key){
super(key);
//this.cursor; // let the class to have ability having cursor
//this.player; //let the class to have player character
}
preload(){
// in the child class, call this by using super.preload();
}
create(){
// in the child class, call this by using super.create();
}
update(){
// in the child class, call this by using super.update();
}
}
//--------------------------------MODULE: BASE SPRITE CLASS----------------------------------
class BaseSprite extends Phaser.Physics.Arcade.Sprite{
constructor(scene,x,y,texture){
super(scene,x,y,texture);
//----must be altered after creating the object--//
this.frameL={start:0,end:0};;//left //{start:.. , end:..}
this.frameR={start:0,end:0};;//right
this.frameU={start:0,end:0};;//up
this.frameD={start:0,end:0};;//down
this.frameT={start:0,end:0};;//turn
this.frameA={start:0,end:0};;//action
this.baseV=300;
this.baseJump=500;
//---------------------//
this.spriteTexture=texture;
scene.add.existing(this); //add sprite to the scene
scene.physics.add.existing(this); //add body to the sprite and then add it to the scene
}
generateAnims(){//call this after setting frame objects
this.anims.create({
key:'left',
frames:this.anims.generateFrameNumbers(this.spriteTexture,this.frameL),
frameRate:10,
repeat:-1
});
this.anims.create({
key:'right',
frames:this.anims.generateFrameNumbers(this.spriteTexture,this.frameR),
frameRate:10,
repeat:-1
});
this.anims.create({
key:'up',
frames:this.anims.generateFrameNumbers(this.spriteTexture,this.frameU),
frameRate:10,
repeat:-1
});
this.anims.create({
key:'down',
frames:this.anims.generateFrameNumbers(this.spriteTexture,this.frameD),
frameRate:10,
repeat:-1
});
this.anims.create({
key:'turn',
frames:this.anims.generateFrameNumbers(this.spriteTexture,this.frameT),
frameRate:20
});
this.anims.create({
key:'action',
frames:this.anims.generateFrameNumbers(this.spriteTexture,this.frameA),
frameRate:10,
repeat:-1
});
}
moveLeft(){
this.setVelocityX(-this.baseV);
this.anims.play('left');
}
moveRight(){
this.setVelocityX(this.baseV);
this.anims.play('right');
}
moveUp(){
this.setVelocityY(-this.baseV);
this.anims.play('up');
}
moveDown(){
this.setVelocitY(this.baseV);
this.anims.play('down');
}
moveJump(){
this.setVelocityY(-this.baseJump);
this.anims.play('up');
}
}
//-------------------------------------MODULE: FUNCTION TO MAKE PLAYER---------------------------------------------
let makePlayer=function(scene,x,y,texture){
let player=new BaseSprite(scene,x,y,texture);
player.frameL={start:12,end:14};//left
player.frameR={start:24,end:26};//right
player.frameU={start:0,end:0};//up
player.frameD={start:0,end:0};//down
player.frameT={start:0,end:0};//turn
player.frameA={start:0,end:0};//action
player.baseV=300;
player.baseJump=500;
player.generateAnims();
}
//--------------------------------------MODULE: CREATE SCENE 1---------------------------------------------
class Scene1 extends BaseScene{
constructor(key='scene1'){
super(key);
this.player;
this.plate
}
preload(){
this.load.image('ground','./assets/tanah_tengah.png');// for testing
this.load.image('sky','./assets/sky.jpg');// for testing
this.load.spritesheet('chibi','./assets/chibis.png',{frameWidth:48,frameHeight:48});// for testing
}
create(){
//add sky background
this.add.image(w/2,h/2,'sky');
//add player
this.player=makePlayer(this,150,50,'chibi'); // THE
// IsParent ERROR
// SEEMS TO
//add plateforms // HAPPENS
this.plate=this.physics.add.staticGroup(); // ARROUND
for(let i=0;i<800;i+=128){ // HERE
this.plate.create(0+i,h-50,'ground'); //
} //
//add collider //
this.physics.add.collider(this.player,this.plate); //
}
update(){
// to be filled
}
};
//---------------------------------MODULE: CONFIG OBJECT-----------------------------------------------------
let setConfigScene=function(s){
this.scene=s;
}
const config={
type:Phaser.AUTO,
width:w,
height:h,
scenes:null,
physics:{
default: 'arcade',
arcade: {
gravity:{y:1000},
debug:true
}
},
setScene:setConfigScene
};
//----------------------------------MODULE: ENTRY POINT-----------------------------------------------------
/*
THIS IS THE ENTRY POINT OF THE GAME
This creates an instance of Phaser.Game class,
using provided config object
and scene files
*/
let scene1=new Scene1(config);
config.setScene(scene1);//set the first scene
const game=new Phaser.Game(config);
Any help is greatly appreciated.
PS: i run it on xampp