ghostrifle Posted February 10, 2014 Share Posted February 10, 2014 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 Quote Link to comment Share on other sites More sharing options...
1-800-STAR-WARS Posted February 10, 2014 Share Posted February 10, 2014 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); Quote Link to comment Share on other sites More sharing options...
xerver Posted February 10, 2014 Share Posted February 10, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.