HelloWorldScene.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import Phaser from 'phaser'
  2. export default class HelloWorldScene extends Phaser.Scene {
  3. private switches?: Phaser.Physics.Arcade.Group;
  4. private switchesA?: Phaser.Physics.Arcade.Group;
  5. private buttons?: Phaser.Physics.Arcade.Group;
  6. private platforms?: Phaser.Physics.Arcade.StaticGroup;
  7. private player?: Phaser.Physics.Arcade.Sprite;
  8. private cursors?: Phaser.Types.Input.Keyboard.CursorKeys;
  9. constructor() {
  10. super('hello-world')
  11. }
  12. preload() {
  13. //this.load.setBaseURL('https://labs.phaser.io')
  14. this.load.image('sky', 'assets/sky.png')
  15. this.load.image("switch", "assets/star.png");
  16. this.load.image("switchA", "assets/bomb.png");
  17. this.load.image("button", "assets/button.png");
  18. this.load.image("buttonA", "assets/buttonA.png");
  19. this.load.image("ground", "assets/platform.png");
  20. this.load.spritesheet("dude", "assets/dude.png",{
  21. frameWidth: 32, frameHeight: 48
  22. });
  23. }
  24. create() {
  25. //Makes sky box
  26. this.add.image(400, 300, 'sky');
  27. // Code Related to platforms
  28. this.platforms = this.physics.add.staticGroup()
  29. const ground = this.platforms.create(400,568, "ground") as Phaser.Physics.Arcade.Sprite
  30. ground.setScale(2)
  31. ground.refreshBody()
  32. //Add Platform(s)
  33. //this.platforms.create(600,400,"ground")
  34. //Code related to the player
  35. this.player = this.physics.add.sprite(100,430, "dude")
  36. this.player.setBounce(0.2)
  37. this.player.setCollideWorldBounds(true)
  38. this.physics.add.collider(this.player, this.platforms)
  39. this.anims.create({
  40. key: "left",
  41. frames: this.anims.generateFrameNumbers("dude", {
  42. start: 0, end: 3
  43. }),
  44. frameRate: 10,
  45. repeat: -1
  46. })
  47. this.anims.create({
  48. key: "turn",
  49. frames: [{key: "dude", frame: 4 }],
  50. frameRate: 20
  51. })
  52. this.anims.create({
  53. key: "right",
  54. frames: this.anims.generateFrameNumbers("dude", {
  55. start: 5, end: 8
  56. }),
  57. frameRate: 10,
  58. repeat: -1
  59. })
  60. this.cursors = this.input.keyboard.createCursorKeys()
  61. //Code related to switches
  62. this.switches = this.physics.add.group({
  63. key: "switch",
  64. setXY: {x:240, y:450}
  65. })
  66. this.physics.add.collider(this.switches, this.platforms)
  67. this.physics.add.overlap(this.player, this.switches, this.handleHitSwitch, undefined, this)
  68. this.switchesA = this.physics.add.group({
  69. key: "switchA",
  70. setXY: {x:240, y:450}
  71. })
  72. this.physics.add.collider(this.switchesA, this.platforms)
  73. this.physics.add.overlap(this.switchesA, this.switches, this.handleSwitchSetup, undefined, this)
  74. this.physics.add.overlap(this.switchesA, this.player, this.handleHitSwitchA, undefined, this)
  75. this.buttons = this.physics.add.group({
  76. key: "button",
  77. setXY: {x:440, y:450}
  78. })
  79. this.physics.add.collider(this.buttons, this.platforms)
  80. this.physics.add.overlap(this.player, this.buttons, this.handleHitButon, undefined, this)
  81. }
  82. //Handle buttons
  83. private handleHitButon(player: Phaser.GameObjects.GameObject, b:Phaser.GameObjects.GameObject){
  84. }
  85. //Handle switches
  86. private handleSwitchSetup(sA: Phaser.GameObjects.GameObject, s:Phaser.GameObjects.GameObject){
  87. const the_switch = sA as Phaser.Physics.Arcade.Image
  88. the_switch.visible = false
  89. }
  90. private handleHitSwitch(player: Phaser.GameObjects.GameObject, s:Phaser.GameObjects.GameObject){
  91. const the_switch = s as Phaser.Physics.Arcade.Image
  92. the_switch.disableBody(true,true)
  93. }
  94. private handleHitSwitchA(player: Phaser.GameObjects.GameObject, sA:Phaser.GameObjects.GameObject){
  95. const the_switch = sA as Phaser.Physics.Arcade.Image
  96. the_switch.visible = true
  97. }
  98. update(){
  99. if(!this.cursors){
  100. return
  101. }
  102. if(this.cursors?.left.isDown){
  103. this.player?.setVelocityX(-160)
  104. this.player?.anims.play("left", true)
  105. }
  106. else if(this.cursors?.right.isDown){
  107. this.player?.setVelocityX(160)
  108. this.player?.anims.play("right", true)
  109. }
  110. else{
  111. this.player?.setVelocityX(0)
  112. this.player?.anims.play("turn")
  113. }
  114. if(this.cursors.up?.isDown && this.player?.body.touching.down){
  115. this.player.setVelocityY(-330)
  116. }
  117. }
  118. }