jflowers45 Posted February 17, 2014 Share Posted February 17, 2014 Got a question about best practices. in the function where dragging begins I'd like to reference other variables that are in the same game state. someSprite.events.onDragStart.add(this.dragStarting); dragStarting(item) {//in this function I want to access variables from the current State but 'this' no longer refers to the game state} i've gotten around this as follows: someSprite.activeState=this;someSprite.events.onDragStart.add(this.dragStarting);dragStarting(item) {//reference item.activeState here} This works fine but I'm wondering whether this is the standard approach or if there's a better way. Thanks-Joe Link to comment Share on other sites More sharing options...
1-800-STAR-WARS Posted February 17, 2014 Share Posted February 17, 2014 xerver made an interesting post about this in the Pixi forum: http://www.html5gamedevs.com/topic/3892-getting-mixed-up-using-this-scopes/?p=24684 I've almost exclusively stuck with using Function.prototype.bind to manage scope, but it seems like you have to be careful (and indeed the bind process is one of the most time consuming if I profile my games). Otherwise, keeping a reference to the object you want a layer above the callback's scope is fine. Link to comment Share on other sites More sharing options...
rich Posted February 17, 2014 Share Posted February 17, 2014 someSprite.events.onDragStart.add(this.dragStarting); dragStarting(item) {//in this function I want to access variables from the current State but 'this' no longer refers to the game state} You should define the scope in your onDragStart line, like so:someSprite.events.onDragStart.add(dragStarting, this);This sets the context in which dragStarting is run, giving you full access to that context (in your case 'this'). Link to comment Share on other sites More sharing options...
jflowers45 Posted February 17, 2014 Author Share Posted February 17, 2014 [EDIT] - Rich beat me to the punch - awesome, thanks Rich Link to comment Share on other sites More sharing options...
Recommended Posts