Jump to content

external module usage of "this"


espace
 Share

Recommended Posts

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

 Share

  • Recently Browsing   0 members

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