ibb671 Posted June 16, 2016 Share Posted June 16, 2016 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 More sharing options...
ClumsyBird Posted June 17, 2016 Share Posted June 17, 2016 U didn't update this.level before u update sprite position. Link to comment Share on other sites More sharing options...
in mono Posted June 17, 2016 Share Posted June 17, 2016 Is some kind of an error thrown somewhere? Link to comment Share on other sites More sharing options...
ibb671 Posted June 17, 2016 Author Share Posted June 17, 2016 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"); Link to comment Share on other sites More sharing options...
Recommended Posts