Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

help with loading next level


ibb671
 Share

Recommended Posts

Hello if anybody can help. I'm trying to load my game to the next level and I read that its better to use a json object instead of having many level states. I'm still learning programming and I'm having a hard time to get it to work. Thank you guys in advancedloading levels.png

 init:function(currentLevel){
 
      //level data
      this.numLevels = 3;
      this.currentLevel = currentLevel ? currentLevel : 1;
      console.log("Current level " + this.currentLevel);
      
      this.won = false;
  
  },


loadLevel:function(){
    this.levelData = {
      "playerPositions":[ {
            "key":"panda",x:600,y:500,"scale":0.3
          },
          {
            "key":"snake","x":200,"y":500,"scale":0.3
          },
          {
            "key":"parrot","x":400,"y":500,"scale":0.3
          }],
          //the next level
           "playerPositions":[ {
            "key":"snake",x:500,y:500,"scale":0.3
          },
          {
            "key":"panda","x":100,"y":500,"scale":0.3
          },
          {
            "key":"parrot","x":300,"y":500,"scale":0.3
          }]
    };
    //load player sprites
    if(this.currentLevel<this.numLevels && this.won){
      this.currentLevel++;
      this.state.start('game', true, false,2);
      this.loadPlayers();
      
    }else{
      this.currentLevel=1;
    }
    
    this.loadPlayers();
    
  },
  
  
  loadPlayers:function(){
    //add group and physics
    this.playerGroup = this.game.add.group();
    this.playerGroup.enableBody = true;
    console.log("im in the loadplayer function");
    
    var currentLevelData = this.levelData[this.currentLevel];
    
    var player;
    //load player to screen with foreach
    this.levelData.playerPositions.forEach(function(element){
      player = this.game.add.sprite(element.x,element.y,element.key);
      player.scale.setTo(element.scale);
      this.playerGroup.add(player);
      player.inputEnabled = true;
      player.input.enableDrag();
    },this);
    
    
  }
  

 

Link to comment
Share on other sites

What problems were you running into exactly?  I'm not sure if you should advance the level (and the restart the state) within the loadLevel method, but maybe you are doing that just for testing.

 

I do think you'll want to change

this.state.start('game', true, false,2);

to

this.currentLevel++;
this.state.start('game', true, false, this.currentLevel);

or maybe just

this.state.start('game', true, false, this.currentLevel + 1);

 

Link to comment
Share on other sites

52 minutes ago, kevinleedrum said:

 

What problems were you running into exactly? I'm not sure if you should advance the level (and the restart the state) within the loadLevel method, but maybe you are doing that just for testing.

 

Hi 

So sorry im new to phaser and programming in general. How should a simple loadLevel method be like? I'm learning phaser through a online video tutorial and i happened to just use a load next level code from there. 

Thank you.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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