Jump to content

Help with updating player position


ibb671
 Share

Recommended Posts

Hello I'm practicing how to load sprite positions from a levelData variable, I'm trying to figure out how to change the position of a sprite with a click. And i'm having trouble figuring it out. The position is not being updated. Please help.

Thank you 

var gameState = {
  init:function(){
      this.currentLevel=0;
  },
  preload:function(){
      
      game.load.image("pokeball","Pokeball.PNG");
  },
  
  create:function(){
      console.log("in create method");
      levelData=[
            {
                playerPosition:{x:200,y:20}
            },
            {
                playerPosition:{x:100,y:200}
            }
          
          ];
          
    
     this.level=levelData[this.currentLevel];
     this.pokeballSprite=this.game.add.sprite(this.level.playerPosition.x,this.level.playerPosition.y,"pokeball");
     this.pokeballSprite.scale.setTo(0.3);
     this.pokeballSprite.inputEnabled = true;
     
     this.pokeballSprite.events.onInputDown.add(this.clicked,this);
      
          
          
  },
  update:function(){
      
  },
  clicked:function(){
      this.currentLevel++;
      
      console.log(this.currentLevel);
      console.log("i clicked it");
      this.pokeballSprite.x=this.level.playerPosition.x;
      this.pokeballSprite.y=this.level.playerPosition.y;
  }
  
  
  
  
    
};


var game = new Phaser.Game(320,480,Phaser.AUTO);
game.state.add("game",gameState);
game.state.start("game");

 

Link to comment
Share on other sites

hello, actually no errors. But I was able to get it to work by doing the following code. But now I ended up having trouble to loop through the "enemyPosition" to be added to the game. I tried forEach loop and had an error. Any advice

 

var   levelData=[
            {
                playerPosition:{x:200,y:20},
                enemyPosition:[{x:100,y:20},{x:150,y:100}]
            },
            {
                playerPosition:{x:100,y:200},
                enemyPosition:[{x:200,y:20},{x:300,y:100}]
            },
            {
                playerPosition:{x:150,y:100},
                enemyPosition:[{x:100,y:20},{x:150,y:100}]
            }
          
          ];


var gameState = {
  init:function(currentLevel){
      this.currentLevel=this.currentLevel || 0;
  },
  preload:function(){
      
      game.load.image("pokeball","Pokeball.PNG");
      game.load.image("mushroom","mushroom.gif");
  },
  
  create:function(){
      console.log("in create method");
  
          
          //init enemy
          this.enemies=this.add.group();

              
     this.scheduleNextEnemy();
          
    
     this.level=levelData[this.currentLevel];
     this.pokeballSprite=this.game.add.sprite(this.level.playerPosition.x,this.level.playerPosition.y,"pokeball");
     this.pokeballSprite.scale.setTo(0.3);
     this.pokeballSprite.inputEnabled = true;
     
     this.pokeballSprite.events.onInputDown.add(this.clicked,this);
         
          
  },
  update:function(){
      if(this.currentLevel==2)
      {
          this.currentLevel=-1;
      }
      
  },
  clicked:function(){
      this.currentLevel++;
      
      console.log(this.currentLevel);
      console.log("i clicked it");
    //   this.pokeballSprite.x=this.level.playerPosition.x;
    //   this.pokeballSprite.y=this.level.playerPosition.y;
      
      this.game.state.start("game",true,false,this.currentLevel);
  },
  
  
  scheduleNextEnemy:function(){
      var enemyIndex = 0
          var enemyData = levelData[enemyIndex].enemyPosition[enemyIndex];
        var enemySprite;
        if(enemyData)
        {
            // enemySprite=this.game.add.sprite(enemyData.x,enemyData.y,"mushroom");
            // this.enemies.add(enemySprite);
            
            enemyData.forEach(function(element,index){
                this.game.add.sprite(element.x,element.y,"mushroom");
                 enemyIndex++;
            },this);
            
            
        }
                 
  }

    
};


var game = new Phaser.Game(320,480,Phaser.AUTO);
game.state.add("game",gameState);
game.state.start("game");



 

 

Screenshot 2016-06-17 at 6.30.19 PM.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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