TypeError: Cannot read properties of undefined (reading 'isParent')

It was working before, I don’t know what happened. Anyways, I got the following error:

Uncaught TypeError: Cannot read properties of undefined (reading ‘isParent’)

The code for the function is

changeBlock(player, blocks) {
     var Random = Phaser.Math.Between(1, 4);
    
     if (Random < 3)
       if (player[id].y > blocks.y + 40)
         if (blocks.texture.key == "play_block") {
       
           //this.sound.play("brick");
           blocks.setTexture("dud_block");
           coin = this.physics.add.sprite(blocks.x, blocks.y - 40, "coin");
           coin.setBounce(1);
           coin.body.setGravityY(300);
           this.physics.add.collider(coin, platforms);
           this.physics.add.collider(player[0], coin, this.collectCoin);
           coin.setVelocityX(60);
           coin.setVelocityY(-300);
           coin.setCollideWorldBounds(true);
         }
     if (Random == 3)
       if (player[id].y > blocks.y + 40)
         if (blocks.texture.key == "play_block") {
           this.sound.play("brick");
           coin = this.physics.add.sprite(blocks.x, blocks.y - 40, "coin");
           coin.setBounce(1);
           coin.body.setGravityY(300);
           this.physics.add.collider(coin, platforms);
           this.physics.add.collider(player[0], coin, this.collectCoin);
           coin.setVelocityX(60);
           coin.setVelocityY(-300);
           coin.setCollideWorldBounds(true);
         }
     if (Random == 4)
       if (player[id].y > blocks.y + 40)
         if (blocks.texture.key == "play_block") {
           this.sound.play("brick");
           blocks.destroy();
         } else {
           this.sound.play("brick");
         }
   }


I will post more information if necessary, thank you.

The reading ‘isParent’ error may come from passing undefined into a collider.

The reading ‘add’ error means this.physics.add is undefined in changeBlock(), probably because the this value is incorrect.

Thank you but I still don’t understand.
What is wrong with the this value and what should I replace it with?

Where are you passing the changeBlock function?

After the update() function

Show that please.

Oh boy where here it goes…

`async update(){//calls itself constantly

  await player;
  var promises;
 
x++
try{

    if (x<3)//so it won't create more than one room, however its stops the rest of the code from being called constantly
    {
    room=this.room()  
    promises=new Promise((resolve)=>{resolve(room)})

   }
   await promises

   promises.then((room)=>{
      
       console.log("success")
   //room.send('x',player.x)
   //room.send('y',player.y)
   
   room.send('player',1)
   
   
   
   room.onMessage('player', (message)=>{
    if (id==0){
     id=message
    }
    console.log("the id is:"+id)
})

   room.onMessage('x', (message)=>{
       console.log(message)
       player[id].x=message;
   })
   room.onMessage('y', (message)=>{
       console.log(message)
       player[id].y=message
   })

   room.onMessage('coin',(message)=>{

    scoreText
    console.log(message)
    score = message;
    scoreText.setText("player1: " + score);
   })
   room.onMessage('coin2',(message)=>{

    scoreText2
    console.log(message)
    score2 = message;
    scoreText2.setText("player2: " + score2);
   })  

/*room.onMessage(‘player2’, (message) => {

       })
    room.onMessage('type', (message) => {
       this.input.keyboard.on('type',evt =>{
           console.log(evt.key)
    player.setY(message)
    console.log(message)
    
    })
    
    })*/

    document.addEventListener("keydown", function(event) {
       if (event.code === 'ArrowRight') {
            player[id].x+=6
            room.send('x',player[id].x)
            console.log(room)
       }
   });
   document.addEventListener("keydown", function(event) {
       if (event.code === 'ArrowLeft') {
            player[id].x-=6
            room.send('x',player[id].x)
            console.log(room)
       }
   });` `

   button_right=this.add.image(270, 400,"button_right")
   .setInteractive()
   .on('pointerdown', ()=> {
     console.log("success")
     r=1
     l=0
 })
   button_left=this.add.image(70, 400,"button_left")
   .setInteractive()
   .on('pointerdown', () =>{
     console.log("success")
     l=1
     r=0
     player[id].anims.play("left", true);
      })
   button=this.add.image(650, 400,"button")
     .setInteractive()
     .on('pointerdown', () =>{
       console.log("success");
       this.sound.play("jump");
       
             })

             document.addEventListener("keydown", function(event) {
       let interval;
        var audio = new Audio('assets/jump.ogg');
       audio.play();
       if (event.code === 'ArrowUp' && player[id].body.touching.down)
           {
               player[id].setVelocityY(-430)
               function jump(){
                   if (player[id].body.touching.down==false){
                       room.send('y',player[id].y)
                       console.log(room) 
                       console.log(player[id].body.touching.down)
                   }
               }
              
               interval=setInterval(()=>{
                 jump()
                 if (player[id].body.touching.down==true)
                 {
                  clearInterval(interval)
                 }},50)
          
           }
   });
   
    })
    var b=0
    b++
    if (b>2)
    {this.collectCoin(player[id],coin,promises)}

}
catch(err){}

const cursors = this.input.keyboard.createCursorKeys();
if (this.input.activePointer.isDown )//while the button is pressed
{
  if (r==0 && l==1){
   
   player[id].x-=2
  // player.flipX = true;
   punching = false;
  
  }
  else if (l==0 && r==1)
 {
  
     player[id].x+=2;
   //  player.flipX = false;
     punching = false;
 }
 
 else
 {}

}

 var  spacebar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);

 button
 .setInteractive()
 .on('pointerdown', () =>{
   player[id].setVelocityY(-430);
   punching = false;
         })
       

  
 if (cursors.left.isDown) {
   //player.setVelocityX(-160);
   player[id].flipX = true;
   player[id].anims.play("left", true);
   punching = false;
 } else if (Phaser.Input.Keyboard.JustDown(spacebar))
   {
   console.log(player[0].anims.getProgress(spacebar));
   player[id].setVelocityX(0);
   player[id].anims.play("punch");
   punching = true;
 } else if (cursors.right.isDown){
   console.log("right");
   //player.setVelocityX(160);
   player[id].flipX = false;
   player[id].anims.play("right", true);
   punching = false;
 } else {
   if (punching==false){
   player[id].anims.play("turn", true);
   }
   player[id].setVelocityX(0);
 }
 /*if (cursors.up.isDown && player.body.touching.down) {
  player.setVelocityY(-430);
   this.sound.play("jump");
   punching = false;
 }*/
 if (initialTime<=0)//time goes to 0
 {
  
   this.scene.start('gameover')
   this.scene.remove('game')
 }
 this.physics.add.collider(player[1], coin, this.collectCoin2,callback,this);

changeBlock(player, blocks) {
var Random = Phaser.Math.Between(1, 4);

 if (Random < 3)
   if (player[id].y > blocks.y + 40)
     if (blocks.texture.key == "play_block") {
   
       //this.sound.play("brick");
       blocks.setTexture("dud_block");
       coin = this.physics.add.sprite(blocks.x, blocks.y - 40, "coin");
       coin.setBounce(1);
       coin.body.setGravityY(300);
       this.physics.add.collider(coin, platforms);
       this.physics.add.collider(player[0], coin, this.collectCoin);
       coin.setVelocityX(60);
       coin.setVelocityY(-300);
       coin.setCollideWorldBounds(true);
     }
 if (Random == 3)
   if (player[id].y > blocks.y + 40)
     if (blocks.texture.key == "play_block") {
       this.sound.play("brick");
       coin = this.physics.add.sprite(blocks.x, blocks.y - 40, "coin");
       coin.setBounce(1);
       coin.body.setGravityY(300);
       this.physics.add.collider(coin, platforms);
       this.physics.add.collider(player[0], coin, this.collectCoin);
       coin.setVelocityX(60);
       coin.setVelocityY(-300);
       coin.setCollideWorldBounds(true);
     }
 if (Random == 4)
   if (player[id].y > blocks.y + 40)
     if (blocks.texture.key == "play_block") {
       this.sound.play("brick");
       blocks.destroy();
     } else {
       this.sound.play("brick");
     }

}
}
`

I don’t see how changeBlock() is getting called.

Usually add.collider() shouldn’t be in update().

async update seems like a real can of worms.

1 Like