TextButtonObject.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * Button class heavily based on
  3. * https://snowbillr.github.io/blog//2018-07-03-buttons-in-phaser-3/
  4. */
  5. // this is a 'text' button
  6. // x location middle but you can change constructor to pass x value
  7. export class TextButtonObject extends Phaser.GameObjects.Text {
  8. constructor(scene: Phaser.Scene, y: number, text: string, callback: () => void) {
  9. super(scene, scene.scale.width / 2, y, text, {});
  10. console.log("in text button - " + text)
  11. this.setInteractive({ useHandCursor: true })
  12. .on('pointerover', () => this.enterButtonHoverState())
  13. .on('pointerout', () => this.enterButtonRestState())
  14. .on('pointerdown', () => this.enterButtonActiveState())
  15. .on('pointerup', () => {
  16. this.enterButtonHoverState();
  17. callback();
  18. });
  19. }
  20. enterButtonHoverState() {
  21. this.setStyle({ fill: '#ff0' });
  22. }
  23. enterButtonRestState() {
  24. this.setStyle({ fill: '#0f0' });
  25. }
  26. enterButtonActiveState() {
  27. this.setStyle({ fill: '#0ff' });
  28. }
  29. }