HollyBlade Posted March 1, 2018 Share Posted March 1, 2018 I have a question. I'm creating multiple "popups" in my game that's triggered with different buttons. Right now I have 5 functions 4 of them are basically the same thing. Closewindow, openwindow, closewindow1 and openwindow1. The only difference is that they correspond to different popups. Is there a more elegant solution (if that's the right word) than what I have right now. I plan on adding more so I feel like there's another way I just don't know what it is. I've looked at groups but don't items in groups have to be all doing the same thing? I attached my code if it helps. Thanks Link to comment Share on other sites More sharing options...
Kosmoon Posted March 1, 2018 Share Posted March 1, 2018 function windowCloserOpener(_window,action){ if ((tween && tween.isRunning) || _window.scale.x === 1){ return; } if (action === 'open'){ tween = game.add.tween(_window.scale).to ( { x:1, y: 1}, 1000; Phaser.Easing.Elastic.Out, true); }else if (action ==='close') { ...... } } //and then make the callbacks like that: popup2.events.onInputDown.add(function(){windowCloserOpener(popup2,'open')},this); // you need to call your function in another function in order to pass it parameters Link to comment Share on other sites More sharing options...
in mono Posted March 2, 2018 Share Posted March 2, 2018 Reuse the code and just pass what's different as a parameter. Better yet, make a class for the popups, write the open and close methods for it, then work with instances of that class. Fenopiù 1 Link to comment Share on other sites More sharing options...
HollyBlade Posted March 2, 2018 Author Share Posted March 2, 2018 I'm a little confused, I'm pretty new to phaser. The buttons are the ones that open and close the individual popups and you can click on the popups to close them. So should that be done with the buttons instead? Link to comment Share on other sites More sharing options...
Fenopiù Posted March 5, 2018 Share Posted March 5, 2018 In your function put sender as parameter, in that case it will be the button clicked, so you can have just one function to manage them all. function openWindow(sender){...} Link to comment Share on other sites More sharing options...
Recommended Posts