{"id":161,"date":"2026-07-15T14:48:22","date_gmt":"2026-07-15T05:48:22","guid":{"rendered":"https:\/\/tate.stars.ne.jp\/wpa\/?p=161"},"modified":"2026-07-15T14:48:45","modified_gmt":"2026-07-15T05:48:45","slug":"myplayer2gamescene","status":"publish","type":"post","link":"https:\/\/tate.stars.ne.jp\/wpa\/myplayer2gamescene.html","title":{"rendered":"myPlayer2GameScene"},"content":{"rendered":"<pre><code class=\"swift\">\nimport * as Phaser from 'phaser';\nimport {Player} from '.\/Player';\nimport { Bullet } from '.\/Bullet';\nimport {ScoreText} from \".\/ScoreText\";\nimport { HeartsManager } from '.\/HeartsManager';\nimport { Boss2 } from '.\/Boss2';\nimport { BossBullet } from '.\/BossBullet';\n\nexport class GameScene extends Phaser.Scene {\nprivate map!: Phaser.Tilemaps.Tilemap;\nprivate tileset!: Phaser.Tilemaps.Tileset;\nprivate tiles!: Phaser.Tilemaps.Tileset;\nprivate Foreground!: Phaser.Tilemaps.TilemapLayer;\nprivate player!: Player;\nprivate joystick: any;\nprivate bullets!: Phaser.Physics.Arcade.Group;\nprivate rexVirtualJoystick: any;\nprivate joystickArea!: Phaser.Geom.Rectangle;\nprivate lastDirection: Phaser.Math.Vector2 = new Phaser.Math.Vector2(1, 0);\nprivate scoreText!: ScoreText;\nprivate heartsManager!: HeartsManager;\nprivate hitCount: number = 0;\nprivate isGameOver: boolean = false;\nprivate boss: Boss2 | null = null;\nprivate playerHp: number = 5;\n constructor() {\n    super({ key: 'GameScene' });\n}\npreload() {\nthis.load.tilemapTiledJSON('map', 'assets\/tilemap.json');\nthis.load.image('tile', 'assets\/tilesets.png');\nthis.load.spritesheet('player', 'assets\/player.png', {\n frameWidth: 64, frameHeight: 64\n });\nthis.load.image('shoot', 'assets\/myshoot.png');\nthis.load.image('bullet', 'assets\/bullet.png');\nthis.load.image('heart', 'assets\/heart.png');\nthis.load.image('boss', 'assets\/boss.png');\nthis.load.image('bossbullet', 'assets\/laser.png');\n}\ncreate() {\nthis.scoreText = new ScoreText(this, 20, 20);\nthis.scoreText.setScrollFactor(0);\nthis.scoreText.setDepth(3);\n\/\/ \u30b9\u30b3\u30a2\u8868\u793a\nconst currentScore = this.registry.get('playerScore');\n this.hitCount = 0;\n this.isGameOver = false;\nif (!this.registry.has('playerLife')) {\n    this.registry.set('playerLife', 10);  \/\/ \u521d\u671f\u30e9\u30a4\u30d5\n}\nconst maxLife = 20; \/\/ \u6700\u5927\u30e9\u30a4\u30d5\u67a0\u6570\nconst currentLife = this.registry.get('playerLife');\n \/\/ \u30cf\u30fc\u30c8\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316 (X: 20, Y: 20 \u306e\u4f4d\u7f6e\u306b\u914d\u7f6e)\n  this.heartsManager = new HeartsManager(this, 500, 20, maxLife, 'heart');\n  this.heartsManager.setDepth(3);\n  this.heartsManager.updateHearts(currentLife);\n  const map = this.make.tilemap({key: 'map'});\n  this.tiles = map.addTilesetImage('tileset', 'tile') as Phaser.Tilemaps.Tileset;\n  const ground = map.createLayer('ground', this.tiles, 0, 0) as Phaser.Tilemaps.TilemapLayer;\n  const Background = map.createLayer('Background', this.tiles, 0, 0) as Phaser.Tilemaps.TilemapLayer;\n  this.Foreground = map.createLayer('Foreground', this.tiles, 0, 0) as Phaser.Tilemaps.TilemapLayer;\n  \/\/  ground.setCollisionByProperty({ collides: true });\n  ground.setCollisionByExclusion([-1], true);\n  this.Foreground.setCollisionByExclusion([-1], true);\n  this.player = new Player(this, 790, 660, 'player');\n  this.physics.add.collider(this.player, ground);\n  this.physics.add.collider(this.player, this.Foreground);\n  this.boss = new Boss2(this, 100, 400);\n  \/\/ \u30dc\u30b9\u306e\u5f3e\u304c\u30d7\u30ec\u30a4\u30e4\u30fc\u306b\u5f53\u305f\u3063\u305f\u6642\u306e\u51e6\u7406\n   this.physics.add.overlap(\n     this.player,\n     this.boss.getBullets(),\n     this.handlePlayerDamage,\n     undefined,\n     this\n );\nthis.joystickArea = new Phaser.Geom.Rectangle(0, 0, 700, 1000);\nconst graphics = this.add.graphics();\ngraphics.lineStyle(2, 0x00ff00, 0);\ngraphics.strokeRectShape(this.joystickArea);\n\/\/ 1. \u30b8\u30e7\u30a4\u30b9\u30c6\u30a3\u30c3\u30af\u306e\u4f5c\u6210\n this.joystick = (this.plugins.get('rexVirtualJoystick') as any).add(this, {\n x: 100,\n y: 400,\n radius: 100,\nbase: this.add.circle(0, 0, 100, 0x0000FF).setAlpha(0.1),\n thumb: this.add.circle(0, 0, 50, 0xFFA500).setAlpha(0.5),\n dir: '8dir',   \/\/ 8\u65b9\u5411\u5165\u529b\n forceMin: 16,\n fixed: false,  \/\/ \u3053\u3053\u3092false\u306b\u8a2d\u5b9a\n enable: true\n }).setVisible(false); \/\/ \u6700\u521d\u306f\u975e\u8868\u793a\u306b\u3057\u3066\u304a\u304f\n this.input.on('pointerdown', (pointer: Phaser.Input.Pointer) => {\n  if (this.joystickArea.contains(pointer.x, pointer.y)) {\n    this.joystick.setPosition(pointer.x, pointer.y);\n    this.joystick.setVisible(true);\n    this.joystick.base.depth = 100; \/\/ \u5fc5\u8981\u306b\u5fdc\u3058\u3066\u91cd\u306a\u308a\u9806\u3092\u8abf\u6574\nthis.joystick.thumb.depth = 101;\n }\n});\n this.joystick.on('pointerup', () => {\n  this.player.setVelocity(0, 0);\n  this.player.anims.stop(); \/\/ \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3092\u505c\u6b62\n\/\/ this.player.setFrame(0);\n });\n\/\/ \u6307\u3092\u96e2\u3057\u305f\u6642\u306b\u975e\u8868\u793a\u306b\u3059\u308b\u5834\u5408\nthis.input.on('pointerup', () => {\n   this.joystick.setVisible(false);\n });\nthis.input.addPointer(2);\n  const jumpButton = this.add.image(850, 880, 'shoot').setScale(0.6).setAlpha(0.5);\n  jumpButton.setInteractive().setScrollFactor(0);\n  jumpButton.on(\"pointerdown\", () => {\n   this.shootBullet();\n });\n jumpButton.on(\"pointerup\", () => {\n    \/\/   this.shoot();\n });\nthis.bullets = this.physics.add.group({\n  classType: Bullet,\n maxSize: 50, \/\/ \u753b\u9762\u5185\u306e\u6700\u5927\u5f3e\u6570\n runChildUpdate: true\n });\nthis.physics.add.collider(this.bullets, this.boss.getBullets(), (bullet, bulletObj) => {\n \/\/ \u578b\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\uff08\u30ad\u30e3\u30b9\u30c8\uff09\u3092\u4f7f\u3063\u3066TypeScript\u306b\u578b\u3092\u4f1d\u3048\u308b\n   (bulletObj as Phaser.Physics.Arcade.Sprite).disableBody(true, true);\n });\n this.physics.add.collider(this.bullets, this.Foreground, (bullet, tileObj) => {\n\/\/ \u578b\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\uff08\u30ad\u30e3\u30b9\u30c8\uff09\u3092\u4f7f\u3063\u3066TypeScript\u306b\u578b\u3092\u4f1d\u3048\u308b\n const tile = tileObj as Phaser.Tilemaps.Tile;\n \/\/ \u885d\u7a81\u3057\u305f\u30bf\u30a4\u30eb\u306e\u5ea7\u6a19\u3092\u6307\u5b9a\u3057\u3066\u524a\u9664\n if (tile) {\n  this.Foreground.removeTileAt(tile.x, tile.y);\n this.scoreText.addScore(10);\n\/\/ \u5fc5\u8981\u306b\u5fdc\u3058\u3066\u5f3e\uff08bullet\uff09\u3082\u6d88\u53bb\n   bullet.destroy();\n }\n });\n this.physics.add.overlap(\n   this.bullets,\n   this.boss,\n (bossObj, bulletObj) => {\n   const bullet = bulletObj as Bullet;\n   const boss = bossObj as Boss2;\n  bullet.setActive(false);\n bullet.setVisible(false);\n  bullet.body!.enable = false;\n\/\/  bullet.disableBody(true, true); \/\/ \u5f3e\u3092\u6d88\u3059\n   boss.takeDamage();\n},\n  undefined,\n this\n );\n }\nprivate handlePlayerDamage(playerObj: any, bulletObj: any) {\nconst bullet = bulletObj as BossBullet;\n let life = this.registry.get('playerLife');\n  if (life > 0) {\n  life--;\n  this.registry.set('playerLife', life); \/\/ registry\u3092\u66f4\u65b0\n this.heartsManager.updateHearts(life);  \/\/ \u898b\u305f\u76ee\u3092\u66f4\u65b0\n }\n if (life === 0) {\n this.add.text(450, 240, 'GAME OVER',\n  {fontSize: '80px', color: '#ff0000'}).setOrigin(0.5).setScrollFactor(0);\n   this.time.delayedCall(3000, () => {\n   this.registry.set('playerLife', 10);\n   this.registry.set('score', 0);\n  this.scene.start('GameScene');\n  });\n }\n \/\/ \u5f3e\u3092\u6d88\u6ec5\u3055\u305b\u308b\n bullet.destroy();\n this.player.setAlpha(0.5);\n  this.time.delayedCall(500, () => this.player.setAlpha(1));\n}\n  \/\/ \u30d7\u30ec\u30a4\u30e4\u30fc\u306e\u88ab\u5f3e\u51e6\u7406\nprivate damagePlayer() {\n   this.playerHp--;\n   this.player.setTint(0xff0000);\n   this.time.delayedCall(100, () => this.player.clearTint());\n  if (this.playerHp <= 0) {\n  this.player.disableBody(true, true);\n  console.log(\"\u30b2\u30fc\u30e0\u30aa\u30fc\u30d0\u30fc\");\n }\n }\nprivate triggerGameOver(): void {\nthis.isGameOver = true;\nthis.physics.pause(); \/\/ \u7269\u7406\u6f14\u7b97\u3092\u505c\u6b62\nthis.player.setTint(0x333333); \/\/ \u30d7\u30ec\u30a4\u30e4\u30fc\u3092\u7070\u8272\u306e\u6b7b\u4ea1\u72b6\u614b\u306b\u3059\u308b\n\/\/ \u30b2\u30fc\u30e0\u30aa\u30fc\u30d0\u30fc\u30c6\u30ad\u30b9\u30c8\nthis.add.text(450, 300, 'GAME OVER', {\n   fontSize: '74px',\n   color: '#ff0000',\n   backgroundColor: '#000000'\n  }).setOrigin(0.5);\n }\n private shootBullet() {\n  \/\/ \u30d7\u30fc\u30eb\u304b\u3089\u975e\u30a2\u30af\u30c6\u30a3\u30d6\u306a\u5f3e\u3092\u53d6\u5f97\nconst bullet = this.bullets.get(this.player.x, this.player.y).setScale(0.15) as Bullet;\nif (bullet) {\n  bullet.setDepth(3);\n \/\/  if (this.lastDirection.x!== 0 &#038;&#038; this.lastDirection.y!== 0) {\n  \/\/ return; \/\/ \u659c\u308145\u5ea6\u65b9\u5411\u306b\u306f\u5f3e\u3092\u98db\u3070\u3055\u306a\u3044\n  \/\/  }\n  \/\/ \u30d7\u30ec\u30a4\u30e4\u30fc\u306e\u73fe\u5728\u4f4d\u7f6e\u304b\u3089\u3001\u6700\u5f8c\u306b\u8a18\u61b6\u3057\u305f\u65b9\u5411\u306b\u5411\u3051\u3066\u767a\u5c04\nbullet.fire(this.player.x-2, this.player.y-3, this.lastDirection);\n  const angle = Math.atan2(this.lastDirection.y, this.lastDirection.x);\n   \/\/ 3. \u5f3e\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u56de\u8ee2\u89d2\u5ea6\u3092\u8a2d\u5b9a\uff08\u203b\u5f3e\u306e\u5143\u753b\u50cf\u304c\u300c\u53f3\u5411\u304d\u300d\u3067\u3042\u308b\u3053\u3068\u304c\u524d\u63d0\uff09\n   bullet.setRotation(angle);\n }\n }\n update() {\n   if (this.boss) {\n      this.boss.update();\n }\nconst cursors = this.joystick.createCursorKeys();\nconst moveDirection = new Phaser.Math.Vector2(0, 0);\nthis.player.setVelocity(0);\nconst speed = 180;\nlet vX = 0, vY = 0;\n\/\/ \u6c34\u5e73\u65b9\u5411\u306e\u79fb\u52d5\n if (cursors.left.isDown) {\n   vX = -speed;\n    moveDirection.x = -1;\n }\nelse if (cursors.right.isDown) {\n  vX = speed;\n  moveDirection.x = 1;\n }\n \/\/ \u5782\u76f4\u65b9\u5411\u306e\u79fb\u52d5\n if (cursors.up.isDown ) {\n   vY = -speed;\n   moveDirection.y = -1;\n } else if (cursors.down.isDown) {\n    vY = speed;\n   moveDirection.y = 1;\n }\n if (moveDirection.length() > 0) {\n   moveDirection.normalize();\n   \/\/ \u79fb\u52d5\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u305d\u306e\u65b9\u5411\u3092\u6700\u65b0\u306e\u5411\u304d\u3068\u3057\u3066\u8a18\u61b6\n   this.lastDirection.copy(moveDirection);\n}\n \/\/ \u30d7\u30ec\u30a4\u30e4\u30fc\u304c\u659c\u3081\u65b9\u5411\u306b\u901f\u304f\u79fb\u52d5\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u901f\u5ea6\u3092\u8a2d\u5b9a\u3057\u3066\u6b63\u898f\u5316\u3057\u307e\u3059\n\/\/   this.player.setVelocity(vX, vY).body.velocity.normalize().scale(speed);\n const body = this.player.body as Phaser.Physics.Arcade.Body;\n this.player.setVelocity(vX, vY);\n body.velocity.normalize().scale(speed);\n\/\/ \u9069\u5207\u306a\u30d7\u30ec\u30a4\u30e4\u30fc\u306e\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3092\u9078\u629e\u3059\u308b\n if (vX < 0) {\n  this.player.anims.play('left', true);\n this.player.setDepth(4);\n }\n else if (vX > 0){\n  this.player.anims.play('right', true);\n  this.player.setDepth(4);\n }\nelse if (vY < 0){\n this.player.anims.play('up', true);\n this.player.setDepth(4);\n }\nelse if (vY > 0){\n this.player.anims.play('down', true);\n  this.player.setDepth(2);\n }\n }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import * as Phaser from &#8216;phase&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-phaser4","category-typescript"],"_links":{"self":[{"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":1,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":162,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/posts\/161\/revisions\/162"}],"wp:attachment":[{"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}