Jump to content

on collide with another object cannot call another scene


Viktoria
 Share

Recommended Posts

I am writing a function for collider. In the function should be checked if the player is still alive. When yes then the live will be decreased and one "heart" symbol from the lives-pannel will be deleted(the player has 3 lives aka heart symbols). The function works perfectly till the place where the player has no lives. My idea was when the player has no more lives than the game should be stopped and the intro scene should be called. But every time no matter what I do I get an Error:


class Level extends Phaser.Scene{

    constructor(){
        super({
            key:"Level"
        });
    }
    
    preload(){
        this.load.spritesheet('kaefer','src/assets/bug.png',{ frameWidth: 55, frameHeight: 56 });
        this.load.spritesheet('butterfly','src/assets/butterfly.png',{ frameWidth: 55, frameHeight: 56 });
        this.load.image('heart','src/assets/heart.png');
        this.load.json('level','src/scenes/levels.json');
        this.load.image('arbeitsbiene','src/assets/arbeitsbiene.png');

    }
    //take the data from Menu Level
    init(data){
        this.player_type = data.player;
    }

    create(){

        //configuration for player
        let player_config = {
            x: this.levelData.playerStart.x,
            y: this.levelData.playerStart.y,
            name: this.player_type,
            cursors: this.input.keyboard.createCursorKeys()
        }

        //create player
        this.bug = new Kaefer(this, player_config);
        this.bug.setCollideWorldBounds(true);
        this.physics.add.collider( this.bug,this.platforms);
        
        //create hearts bar
        this.lifes = this.physics.add.staticGroup();
        for(let i = 0; i< this.bug.lives; i++){
           let j = i + 1;
            this.lifes.create(35 * j, 20, 'heart');
        }
        console.log(this.lifes);
        //
        let bee = {
            x: 350,
            y: 400,
            name: 'arbeitsbiene',
           //facing_left: false,
            speed: 150,
            jump: 340
        }

        this.bee = new ArbeitsBiene(this, bee);
        this.physics.add.collider(this.bee,this.platforms);

        //this.physics.add.collider(this.bee, this.ground);
        this.bee.setCollideWorldBounds(true);
        this.physics.add.overlap(this.bee, this.bug, hitBee, null, this);
        
         function hitBee(bee,bug){

             let heart = this.lifes.getChildren();// gets an array if children  [entr0,entr1,entr2]
             let alive = this.bug.isAlive();//the method from bug Class. Checks if a bug is still alive

             if(alive){
                 heart[heart.length-1].destroy();//destroyes one heart symbol
                 this.bug.setX(this.levelData.playerStart.x);
                 this.bug.setY(this.levelData.playerStart.y-100);
                 this.bug.lives = this.bug.lives - 1;
             }else{
                this.scenee.start('Menu');
             }
         }
    update(){
       this.bug.move();
       this.bee.move();
    }
}



image.png.5a59eb8b0b96e0fd8fe1f05526f717d9.png

Link to comment
Share on other sites

Yes..but that was not a problem. Thank you for response. It turned out that an error was shown because despite the player was dead - an update() method called bug.move() method and as a result an error occured. in update() method it should be checked if a bug is alive. If yes then move() method should be called but if not then - do nothing.?

update(){
        if(this.bug.isAlive()){
            this.bee.move();
            this.bug.move();
        }
    }

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...