lemmikens Posted November 6, 2018 Share Posted November 6, 2018 Hi there, I'm trying to create an image and a tween to go along with it after a couple conditionals and it can't seem to get it right. Any help is greatly appreciated! var locked_in; var tween; this.input.on('drop', function (pointer, gameObject, dropZone) { gameObject.x = dropZone.x; gameObject.y = dropZone.y-60; dropZone.clearTint(); //makes it so 'mans' can still receive input after drop gameObject.input.enabled = true; //heroDown = true; clickButton .setInteractive() .on('pointerdown', function(pointer){ console.log(startSpots); startSpots.clear(true, true); gameObject.input.enabled = false; locked_in = this.add.image(400,400,'locked_in'); tween = this.tweens.add({ targets: locked_in, x: 400, // '+=100' y: 300, // '+=100' ease: 'Linear', // 'Cubic', 'Elastic', 'Bounce', 'Back' duration: 1000, repeat: 0, // -1: infinity yoyo: false }); } ); Link to comment Share on other sites More sharing options...
prob Posted November 6, 2018 Share Posted November 6, 2018 I'm not sure what you're using to generate your code, but I'd recommend setting up some sort of linting, like ES Lint, with a standard rule set; it'll help format your content to be more readable and make errors and such easier to see. The functions for on 'drop' and 'pointer down' are establishing new scope (this). You can either bind the original scope to the function, or create a reference to it prior and use that. lemmikens 1 Link to comment Share on other sites More sharing options...
lemmikens Posted November 6, 2018 Author Share Posted November 6, 2018 Thanks, appreciate the suggestion. Still pretty new to JS... How exactly do I bind the original scope to the function? Thanks! Link to comment Share on other sites More sharing options...
cornstipated Posted November 6, 2018 Share Posted November 6, 2018 all the callbacks in phaser have an argument after the function that will be used as the scope ie gameobject.on(eventtype,callback,callbackscope) which could be gameobject.on('pointerdown',function(){},this) About your original question: it really doesnt ask what you want to achieve or how the code output differs from what you would expect and on top of that it is not self contained so it cant be executed to reproduce and see the problem (relies on things that are not visible in the snippet). That being said one potential problem I see is adding eventlisteners inside eventlistener callback and i doubt this is intentional. For example every time you drop something you will add an on pointerdown listener to clickbutton so the first time you drop it will have one but the second one it will have two and call the callback twice and so on. Also it is totally unnecessary to setinteractive there, you could just do it when you create the button. lemmikens 1 Link to comment Share on other sites More sharing options...
lemmikens Posted December 5, 2018 Author Share Posted December 5, 2018 Adding the .bind() method to each conditional allowed me to use "this" inside of them: var locked_in; //var tween = var tween; this.input.on('drop', function (pointer, gameObject, dropZone) { gameObject.x = dropZone.x; gameObject.y = dropZone.y-60; dropZone.clearTint(); //makes it so 'mans' can still receive input after drop gameObject.input.enabled = true; //heroDown = true; clickButton .setInteractive() .on('pointerdown', function(pointer){ locked_in = this.add.image(400,400,'locked_in'); console.log(startSpots); startSpots.clear(true, true); gameObject.input.enabled = false; tween = this.tweens.add({ targets: locked_in, x: 400, // '+=100' y: 300, // '+=100' ease: 'Cubic', // 'Cubic', 'Elastic', 'Bounce', 'Back' duration: 2, repeat: 0, // -1: infinity yoyo: false }); }.bind(this)); }.bind(this)); you can also do something like this = newThis and pass your "newThis" to whatever function you need. Both are helpful Link to comment Share on other sites More sharing options...
Recommended Posts