espace Posted October 12, 2016 Share Posted October 12, 2016 hi, i want to work with external module and i would use the correct usage of "this". this snippet works : https://jsfiddle.net/espace3d/hmv7kzqu/ var P= P || {} P.draw = function(game,posx,posy){ var e={} e.character=game.add.sprite(posx,posy,'circle') e.fall=function(){ e.character.y+=1 } return e } P= P || {} var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload,create: create, update: update, render: render }); var player var opponent function preload() { game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png'); } function create() { player=P.draw(game,100,100) opponent=P.draw(game,200,200) } function update() { player.fall() opponent.fall() } function render() { } but when i would use "this", the opponent don't fall. why ? https://jsfiddle.net/espace3d/rjybz54d/ var P= P || {} P.draw = function(game,posx,posy){ this.character=game.add.sprite(posx,posy,'circle') this.fall=function(){ this.character.y+=1 } return this } P= P || {} var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload,create: create, update: update, render: render }); var player var opponent function preload() { game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png'); } function create() { player=P.draw(game,100,100) opponent=P.draw(game,200,200) } function update() { player.fall() //don't work opponent.fall() } function render() { } Link to comment Share on other sites More sharing options...
squilibob Posted October 12, 2016 Share Posted October 12, 2016 To do what you are trying to do, you need to make a new P everytime you declare a new sprite. Your code will work if you declare opponent like this opponent=new P.draw(game,200,200) espace 1 Link to comment Share on other sites More sharing options...
espace Posted October 13, 2016 Author Share Posted October 13, 2016 hi, thanks, my code works https://jsfiddle.net/1kzfenks/ Link to comment Share on other sites More sharing options...
Recommended Posts