espace

external module usage of "this"

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() {
}

 

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.