Jump to content

referencing stage


Recommended Posts

Hi guys,


I have a problem with referencing the stage. Maybe it's  just a JS problem.

My problem lies in the Player.prototype.loaded_normal_animations method. The this.stage reference is gone.

function Player(pixie_stage){  this.stage = pixie_stage;  console.log(this.stage);  this.iMovementDirectionX = 0;  this.iMovementDirectionY = 0;  this.iMovementSpeed = 10;}Player.constructor = Player;Player.prototype = new Object();Player.prototype.load_assets = function(){  var assetsToLoader = [ "data/banshee/normal.json"];    var loader = new PIXI.AssetLoader(assetsToLoader);  loader.onComplete = this.loaded_normal_animations  loader.load();}Player.prototype.loaded_normal_animations = function(){    var normal_animations = [];    normal_animations.push(PIXI.Texture.fromFrame("normal1.png"));    normal_animations.push(PIXI.Texture.fromFrame("normal2.png"));    this.playerSprite = new PlayerShip(normal_animations);    this.stage.addChild(this.playerSprite);    this.playerSprite.position.x = 400;    this.playerSprite.position.y = 500;}

Regards, Alex

Link to comment
Share on other sites


loader.onComplete = this.loaded_normal_animations
I think this would lead to the function being bound to the window, though I'm not sure. Try this:
loader.onComplete Player.prototype.loaded_normal_animations.bind(this);



Your solution is correct :) the event will have the context of the AssetLoader since the callback is called like so: `this.onComplete` within the AssetLoader instance. If you console.log(this) inside your load callback you will see the context is not what you thought. Lewis's solution should fix your issue

Link to comment
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.

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.


  • Recently Browsing   0 members

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