Ninjadoodle Posted May 9, 2018 Share Posted May 9, 2018 Hi @enpu Is there a way I can access the 'moving' variable I pass to a class on creating, from inside its onClick function's tween's onComplete? I'm trying to set the 'moving' var back to false, in the tween onComplete event. game.createClass('S17PuzzleTile', { init: function(image, moving, pos) { this.sprite = new game.Sprite(image); this.sprite.position.set(448, 768); this.sprite.anchorCenter(); this.sprite.interactive = true; this.sprite.addTo(game.scene.mg); this.sprite.mousedown = function() { if (!game.scene.solved) { game.audio.playSound('click.wav'); if (!moving) { moving = true; if (pos === 1) { game.Tween.add(this, { x: 832, y: 768 }, 1000, { easing: 'Quadratic.Out' }).onComplete(function() { moving = false; }.bind(this)).start(); } else if (pos === 2) { game.Tween.add(this, { x: 832, y: 1152 }, 1000, { easing: 'Quadratic.Out' }).onComplete(function() { moving = false; }.bind(this)).start(); } } pos ++; } }; } }); Quote Link to comment Share on other sites More sharing options...
enpu Posted May 9, 2018 Share Posted May 9, 2018 @Ninjadoodle You should really try to avoid nested functions (functions inside functions). Now you have your class init function, inside that you have mousedown function, and inside that onComplete functions. Your code gets really hard to read and understand because of that. Instead create new functions to your class, and bind your functions into those. Like this: game.createClass('S17PuzzleTile', { init: function(image, moving, pos) { // Store variables to class this.moving = moving; this.pos = pos; this.sprite = new game.Sprite(image); this.sprite.position.set(448, 768); this.sprite.anchorCenter(); this.sprite.interactive = true; this.sprite.addTo(game.scene.mg); this.sprite.mousedown = this.mousedown.bind(this); }, mousedown: function() { if (game.scene.solved) return; game.audio.playSound('click.wav'); if (!this.moving) { this.moving = true; if (this.pos === 1) { game.Tween.add(this, { x: 832, y: 768 }, 1000, { easing: 'Quadratic.Out' }).onComplete(this.movingEnd.bind(this)).start(); } else if (this.pos === 2) { game.Tween.add(this, { x: 832, y: 1152 }, 1000, { easing: 'Quadratic.Out' }).onComplete(this.movingEnd.bind(this)).start(); } } this.pos++; }, movingEnd: function() { this.moving = false; } }); Looks a lot cleaner and easier to read? Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted May 9, 2018 Author Share Posted May 9, 2018 Hi @enpu Thank for that! I do understand what you are doing, but it's still a bit over my head. In the mousedown function, I did have to change this to this.sprite in order to make the tween work. I'll try to keep using this method instead of what I was using, hopefully it will become second nature. Quote Link to comment Share on other sites More sharing options...
enpu Posted May 9, 2018 Share Posted May 9, 2018 Ah sorry i missed that one from the code! Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted May 9, 2018 Author Share Posted May 9, 2018 Hi @enpu - That's no problem, I'm grateful for your examples and it makes me think about what I'm doing lol. Thanks heaps again! 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.