{"id":192,"date":"2026-10-25T17:39:34","date_gmt":"2026-10-25T08:39:34","guid":{"rendered":"https:\/\/tate.stars.ne.jp\/wpa\/?p=192"},"modified":"2026-10-25T17:39:35","modified_gmt":"2026-10-25T08:39:35","slug":"joystick2renato-cassino","status":"publish","type":"post","link":"https:\/\/tate.stars.ne.jp\/wpa\/joystick2renato-cassino.html","title":{"rendered":"Joystick2(Renato Cassino)"},"content":{"rendered":"<pre><code class=\"swift\">\nnpm install phaser-virtual-joystick\nimport { VirtualJoystick } from 'phaser-virtual-joystick';\n\nimport * as Phaser from 'phaser';\nimport { VirtualJoystick } from 'phaser-virtual-joystick';\nimport { Bullet } from '.\/Bullet';\nimport { Player } from '.\/Player';\n\nexport class GameScene extends Phaser.Scene {\n private joystick!: VirtualJoystick;\n private player!: Player;\n private playerSpeed: number = 250;\n private bulletsGroup!: Phaser.Physics.Arcade.Group;\n private currentFacingAngle: number = 0;\n private isJoystickActive: boolean = false;\n private guideImage!: Phaser.GameObjects.Image;\n private guideText!: Phaser.GameObjects.Text;\nconstructor() {\n  super({ key: 'GameScene' });\n }\n\npreload() {\n  this.load.spritesheet('player', 'assets\/player.png', {\n     frameWidth: 64, frameHeight: 64\n  });\n  this.load.image('bullet', 'assets\/bullet.png');\n  this.load.image('fireButton', 'assets\/shoot.png');\n  this.load.image('joy_icon', 'assets\/joy_icon.png');\n  this.load.font('myfont', 'assets\/fonts\/JPRegular.ttf', 'opentype');\n}\n\ncreate() {\n this.cameras.main.setBackgroundColor('rgba(166,246,134,0.9)');\n this.cameras.main.setBounds(0, 0, 1280, 704);\n this.physics.world.setBounds(0, 0,  1280, 704);\n this.player = new Player(this, 490, 300);\n this.player.setCollideWorldBounds(true);\n this.player.anims.play('walk-right', true);\n this.bulletsGroup = this.physics.add.group({\n classType: Bullet,\n maxSize: 30, \n runChildUpdate: true \n });\n\n this.input.setTopOnly(false);\n this.guideImage = this.add.image(110, this.scale.height - 150, 'joy_icon');\n this.guideText = this.add.text(330, this.scale.height - 150,\n            `\u753b\u9762\u3092\u30bf\u30c3\u30c1\u3057\u3066\nplayer\u3092\u79fb\u52d5`, { fontSize: '30px', color: '#ff0000' }).setOrigin(0.5);\n\nthis.tweens.add({\n   targets: [this.guideImage, this.guideText],\n   alpha: 0.3,\n   duration: 800,\n   yoyo: true,\n    repeat: -1\n});\n const baseSize = Math.min(this.scale.width, this.scale.height);\n const baseRadius = baseSize * 0.18; \/\/ \u7d04 18% (\u4f8b: 80px)\n const stickRadius = baseSize * 0.08; \/\/ \u7d04 8%  (\u4f8b: 35px)\n const deadZoneRadius = baseSize * 0.03; \/\/ \u7d04 3% (\u4f8b: 11px)\n this.joystick = new VirtualJoystick({\n   scene: this,\n   deadZone: {\n   radius: deadZoneRadius,\n   fillColor: 0x1a1a1a,\n   strokeColor: 0xcccccc,\n   alpha: 0.2\n  },\n  baseArea: {\n  radius: baseRadius,\n  fillColor: 0x1a1a1a,\n  strokeColor: 0xcccccc,\n   alpha: 0.1\n},\n  stick: {\n   radius: stickRadius,\n   fillColor: 0x666666,\n   strokeColor: 0x666666,\n    alpha: 0.5,\n },\n   enableWithoutTouch: true,\n   bounds: {\n   topLeft: { x: 0, y: 10 },\n   bottomRight: {\n     x: this.scale.width * 0.8,\n     y: this.scale.height,\n   },\n},\n stickIcon: this.add.text(-100,30, '?', {\n   fontFamily: 'myfont', \/\/ \u30ed\u30fc\u30c9\u6642\u306b\u6307\u5b9a\u3057\u305f\u30ad\u30fc\u3092\u4f7f\u7528\n   fontSize: 60,\n   color: '#ffffff',\n   metrics: { \n    ascent: 52, \n    descent: 15,\n    fontSize: 60 \/\/ \u30d5\u30a9\u30f3\u30c8\u30b5\u30a4\u30ba\u306e\u5b9f\u5bf8\u30d4\u30af\u30bb\u30eb\n },\n    padding: { top: 10, bottom: 10, left: 10, right: 10 },\n    }).setOrigin(0.5, 0.5),\n});\nthis.joystick.setDepth(2);\n this.add.existing(this.joystick);\n this.joystick.on('move', (data) => {\n   this.isJoystickActive = true;\n    if (this.isJoystickActive) {\n    this.player.handleMovement(data.x, data.y);\n    }\n });\nthis.joystick.on('release', () => {\n   this.stopPlayer();\n});\n this.joystick.on('press', () => {\n   this.tweens.add({\n   targets: [this.guideImage, this.guideText],\n   alpha: 0,\n   duration: 500,\n   onComplete: () => {\n   this.guideImage.destroy();\n   this.guideText.destroy();\n  }\n });\n});\n const buttonX = this.scale.width - 110;\n const buttonY = this.scale.height - 100;\n const fireButton = this.add.image(buttonX, buttonY, 'fireButton')\n    .setInteractive()\n    .setScrollFactor(0)\n    .setDepth(100)\n    .setScale(0.48)\n    .setAlpha(0.5);\n fireButton.on('pointerdown', () => {\n    this.fireBullet();\n     });\n}\n private stopPlayer() {\n  this.player.setVelocity(0, 0);\n     this.player.anims.stop();\n}\nprivate fireBullet() {\n  const bullet = this.bulletsGroup.get(this.player.x, this.player.y + 15).setScale(0.15) as Bullet;\nif (bullet) {\n   bullet.setActive(true);\n   bullet.setVisible(true);\n   bullet.fire(this.player.x, this.player.y, this.player.currentFacingAngle);\n }\n }\n update(time: number, delta: number) {\n   if (!this.isJoystickActive) {\n    this.player.setFrame(9);\n }\n  this.joystick?.update();\n }\n}\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\nimport * as Phaser from 'phaser';\nexport class Bullet extends Phaser.Physics.Arcade.Sprite {\n private speed: number = 700;\nconstructor(scene: Phaser.Scene, x: number, y: number) {\n  super(scene, x, y, 'bullet');\n  scene.add.existing(this);\n  scene.physics.add.existing(this).setDepth(2);\n  scene.time.delayedCall(2000, this.destroyBullet, [], this);\n}\n private destroyBullet(): void {\n    this.destroy();\n     this.setVisible(false);\n }\npublic fire(x: number, y: number, angleRad: number) {\n  this.enableBody(true, x, y, true, true);\n const speed = 700;\n const velocityX = Math.cos(angleRad) * speed;\n const velocityY = Math.sin(angleRad) * speed;\n this.setVelocity(velocityX, velocityY);\n this.setRotation(angleRad); \n}\nupdate() {\n  if (this.y < 0 || this.y > 1676 || this.x < 0 || this.x > 2890) {\n  this.disableBody(true, true);\n  }\n}\n}\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\nimport * as Phaser from 'phaser';\nimport { Bullet } from '.\/Bullet';\nimport {VirtualJoystick} from \"phaser-virtual-joystick\";\n\nexport class Player extends Phaser.Physics.Arcade.Sprite {\n  private playerSpeed: number = 250; \n  private joystick!: VirtualJoystick;\n  currentFacingAngle: number = 0; \nconstructor(scene: Phaser.Scene, x: number, y: number){\n  super(scene, x, y, 'player');\n  scene.add.existing(this);\n  scene.physics.add.existing(this);\n  this.setBounce(0.2).setScale(1.3).setDepth(3);\n  this.setCollideWorldBounds(true); \/\/ \u753b\u9762\u5916\u306b\u51fa\u306a\u3044\u3088\u3046\u306b\u5236\u9650\n  this.setOrigin(0.5, 0.5);\n  this.createPlayerAnimations();\n }\n private createPlayerAnimations() {\n  const directions = [\n   { key: 'walk-down', start: 0, end: 2 },\n   { key: 'walk-left', start: 6, end: 8 },\n   { key: 'walk-up', start: 3, end: 5 },\n   { key: 'walk-right', start: 9, end: 11 },\n   { key: 'walk-downright', start: 21, end: 23 },\n   { key: 'walk-downleft', start: 18, end: 20 },\n   { key: 'walk-upright', start: 15, end: 17 },\n   { key: 'walk-upleft', start: 12, end: 14 },\n   { key: 'idle', start: 9, end: 9 },\n ];\ndirections.forEach(dir => {\n this.anims.create({\n  key: dir.key,\n  frames: this.anims.generateFrameNumbers('player', { start: dir.start, end: dir.end }),\n  frameRate: 10,\n  repeat: -1\n  });\n });\n }\n\npublic handleMovement(x: number, y: number){\n  const distance = Math.sqrt(x * x + y * y);\n if (distance < 0.15) {\n   this.setVelocity(0, 0);\n   this.anims.stop();\n   return;\n  }\n  this.playerSpeed = 0;\n   if (distance >= 0.4) {\n   this.playerSpeed = distance * 400;\n   } else if (distance >= 0.6) {\n     this.playerSpeed = distance * 1200;\n   } else {\n   this.playerSpeed = distance * 150;\n }\n  this.setVelocity(x * this.playerSpeed, y * this.playerSpeed);\n   const angle = Math.atan2(y, x);\n   let degrees = Phaser.Math.RadToDeg(angle);\n   let rounded = Math.round(degrees \/ 45) * 45;\n   rounded = ((rounded % 360) + 360) % 360;\n   if (rounded % 90 !== 0) {\n     switch (rounded) {\n       case 45:  rounded -= 12; break;  \/\/ 50\u5ea6\u306b\u3059\u308b\n       case 135: rounded += 12; break;  \/\/ 130\u5ea6\u306b\u3059\u308b\n       case 225: rounded -= 12; break;  \/\/ 230\u5ea6\u306b\u3059\u308b\n       case 315: rounded += 12; break;  \/\/ 310\u5ea6\u306b\u3059\u308b\n }\n}\n this.currentFacingAngle = Phaser.Math.DegToRad(rounded);\n\/\/ \u6271\u3044\u3084\u3059\u3044\u3088\u3046\u306b 0 \u301c 360 \u5ea6\u306e\u7bc4\u56f2\u306b\u6b63\u898f\u5316\n if (degrees < 0) {\n     degrees += 360;\n   }\nif (degrees >= 337.5 || degrees < 22.5) {\n    this.anims.play('walk-right', true);\n    this.setDepth(4);\n  } else if (degrees >= 22.5 && degrees < 67.5) {\n    this.anims.play('walk-downright', true);\n    this.setDepth(4);\n  } else if (degrees >= 67.5 && degrees < 112.5) {\n    this.anims.play('walk-down', true);\n    this.setDepth(1);\n  } else if (degrees >= 112.5 && degrees < 157.5) {\n    this.anims.play('walk-downleft', true);\n    this.setDepth(4);\n  } else if (degrees >= 157.5 && degrees < 202.5) {\n    this.anims.play('walk-left', true);\n    this.setDepth(4);\n } else if (degrees >= 202.5 && degrees < 247.5) {\n    this.anims.play('walk-upleft', true);\n    this.setDepth(4);\n } else if (degrees >= 247.5 && degrees < 292.5) {\n    this.anims.play('walk-up', true);\n    this.setDepth(4);\n } else if (degrees >= 292.5 && degrees < 337.5) {\n    this.anims.play('walk-upright', true);\n    this.setDepth(4);\n }\n  }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>npm install phaser-virtual-joy&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-192","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\/192","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=192"}],"version-history":[{"count":1,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":193,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/posts\/192\/revisions\/193"}],"wp:attachment":[{"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tate.stars.ne.jp\/wpa\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}