ãã®èšäºã§ã¯ããAudioStreamPlayerãããŒã 1 ã€ã§è€æ°ã®ãµãŠã³ããªãœãŒã¹ïŒé³æ¥œãå¹æé³ã®ãã¡ã€ã«ïŒãåãæ¿ããŠåçããæ¹æ³ã説æããã
åºæ¬çã«ããAudioStreamPlayerãç³»ã®ã¯ã©ã¹ã¯ãããŒãïŒã€ã«å¯ŸããŠãèšå®ã§ãããµãŠã³ããªãœãŒã¹ã¯ïŒã€ã ãã ãã€ãŸãããµãŠã³ããªãœãŒã¹ã®æ°ã ãã·ãŒã³ããªãŒå
ã«ãAudioStreamPlayerãããŒããè¿œå ããªããã°ãªããªãããµãŠã³ããªãœãŒã¹ä»¥å€å
šãŠåãèšå®ã®ããŒããã·ãŒã³ããªãŒå
ã«è€æ°ååšããïŒä»¥äžã®ã¹ã¯ãªãŒã³ã·ã§ããã®ãããªïŒç¶æ³ã«ã¯ç¡é§ãæããã ãããããŸãã·ãŒã³ããã¯ã®èŠèªæ§ãæªããŠå°ãå«ã«ãªããããããªãã
ããã§ãåæã«é³Žããããšããªãè€æ°ã®ãµãŠã³ããªãœãŒã¹ãããå ŽåïŒäŸãã°ããã£ã©ã¯ã¿ãŒã®æ»æã®çš®é¡ã«å¿ããè€æ°ã®å¹æé³ãUIäžã®ãã¿ã³ã®çš®é¡ã«å¿ããè€æ°ã®å¹æé³ãªã©ïŒããããžã§ã¯ãã«ã¯çšéããšã«å¿ èŠæå°éã®ãAudioStreamPlayerãããŒãã ãè¿œå ããŠãããããããã¹ã¯ãªããããç¶æ³ã«å¿ããŠãµãŠã³ããªãœãŒã¹ãåãæ¿ããã°ç¡é§ããªããã·ãŒã³ããã¯ã®èŠãç®ãã¹ãããªããã¯ãã ã
ãšããããšã§ãããããã®ãã¥ãŒããªã¢ã«ã§ã¯ããAudioStreamPlayerãããŒãã¯ïŒã€ã ãçšæããŠãããã«é©çšãããµãŠã³ããªãœãŒã¹ãã¹ã¯ãªããããåãæ¿ããŠãåçã§ããããã«å¶åŸ¡ããŠããã
Environment
Godot ã®ããŒãžã§ã³: 3.5.1
ã³ã³ãã¥ãŒã¿ã®OS: macOS 12.6
Basic Articles
以äžã®èšäºãã圹ç«ãŠãã ããã
Godot ãããŠã³ããŒããã
Godot ã®ãããžã§ã¯ããããŒãžã£ãŒ
Godot ã®èšèªèšå®
æºå
ãããžã§ã¯ãèšå®
æ°èŠãããžã§ã¯ããç«ã¡äžããããããããžã§ã¯ããã¡ãã¥ãŒïŒããããžã§ã¯ãèšå®ããã以äžã®èšå®ãè¡ãªãã
- ãäžè¬ãã¿ã ïŒ ãµã€ãããŒãDisplayãïŒãWindowãïŒãSizeãã»ã¯ã·ã§ã³ã«ãŠããŠã€ã³ããŠãµã€ãºã以äžã®ããã«èšå®ããã
ãµãŠã³ããªãœãŒã¹ã®ããŠã³ããŒã
以äžã®Dropboxã®å ±æãã©ã«ãã« ïŒ ã€ã® .wav ãã¡ã€ã«ïŒå¹æé³ïŒãçšæããŠããã
ããŠã³ããŒããããaudioããã©ã«ãããšãã¡ã€ã«ã·ã¹ãã ããã¯ãžãã©ãã°&ããããããŠãããžã§ã¯ããžè¿œå ããã
ã·ãŒã³ã®äœæ
以äžã®ãããªã·ãŒã³ããªãŒãäœããä»åã¯éå»æé«ã«ã·ã³ãã«ãªããªãŒã§ããã
- UI (Control)
- SoundLabel (Label)
- AudioStreamPlayer
ã·ãŒã³ããªãŒã®äœæãå®äºãããããã¡ã€ã«åããUI.tscnããšããŠä¿åããŠããã
ããŒãã®ç·šé
UI (Control) ã«ãŒãããŒã
ãã®ã«ãŒãããŒãã®ãAnchorããç»é¢ãµã€ãºãšåãã«ãªãããæ¡ããŠãããçç±ã¯ããã®ããšåããŒãã®ãSoundLabelããç»é¢ã®äžå€®ã«é 眮ãããããã ã
- 2D ã¯ãŒã¯ã¹ããŒã¹ã®ããŒã«ããŒãããLayoutãïŒãå
šç»é¢(Full Rect)ããéžæãããããã§ãAnchorãããããã£ã¯èªåçã«èª¿æŽããã2D ã¯ãŒã¯ã¹ããŒã¹äžã以äžã®ããã«ãªãã
SoundLabel (Label) ããŒã
ãã®ããŒãã¯ãçŸåšãAudioStreamPlayerãããŒãã®ãStreamãããããã£ã«é©çšãããŠãããµãŠã³ããªãœãŒã¹ã®ãã¹ã衚瀺ããããã«å©çšãããããã«ããããããžã§ã¯ããå®è¡ããŠåäœç¢ºèªãããæã«ãä»ã©ã®ãªãœãŒã¹ãã¡ã€ã«ãé©çšããã³åçããŠããã®ããã²ãšç®ã§ãããã
- ã€ã³ã¹ãã¯ã¿ãŒã«ãŠãTextãããããã£ã«ã¯äžæŠãno soundããšä»®ã«å
¥ããŠããã
- ãAlignãããããã£ããCenterãã«å€æŽãããããã§ãTextãã®æååãæ°Žå¹³æ¹åã§äžå€®ã«è¡šç€ºãããã
- ãControlãã¯ã©ã¹ã®ãGrow DirectionãïŒãHorizontalãããããã£ããBothãã«å€æŽãããããã§ãTextãã®æåæ°ãå¢ããŠãå·Šå³äž¡æ¹åã«æ¡å€§ããã®ã§ãåžžã«ç»é¢äžå€®ã«è¡šç€ºãããã
- 2D ã¯ãŒã¯ã¹ããŒã¹ã®ããŒã«ããŒãããLayoutãïŒãäžå€®ããéžæããã2D ã¯ãŒã¯ã¹ããŒã¹äžã以äžã®ããã«ãªãã
AudioStreamPlayer ããŒã
ãã®ããŒãã¯ãµãŠã³ããªãœãŒã¹ãé©çšããŠããããåçããããã®ããŒãã ãããŒãã®äœããšããŠãããšããšïŒã€ã®ãµãŠã³ããªãœãŒã¹ããèšå®ã§ããªãä»æ§ã§ããããä»åã®ãã¥ãŒããªã¢ã«ã§ã¯ããã®ããŒãïŒã€ã§ãè€æ°ã®ãµãŠã³ããªãœãŒã¹ãåãæ¿ããŠåçãããããã®å¶åŸ¡ã¯ã®ã¡ã»ã©ã¹ã¯ãªããã«ãŠè¡ãããšã«ãªãããããã§ã¯ã²ãšãŸãããŒãã®èšå®ãããŠããã
- ãã¡ã€ã«ã·ã¹ãã ããã¯ããå ã«ã€ã³ããŒãããŠããããµãŠã³ããªãœãŒã¹ãã¡ã€ã«ã®ãã¡ãres://auido/coin.wavãããã€ã³ã¹ãã¯ã¿ãŒã®ãStreamãããããã£ãžãã©ãã°&ããããããŠé©çšãããã²ãšãŸããããããã©ã«ãã®ãµãŠã³ããªãœãŒã¹ãšããã
UI ã«ãŒãããŒãã«ã¹ã¯ãªãããã¢ã¿ãããã
ããããããµãŠã³ããªãœãŒã¹ãèšå®ãšåçãå¶åŸ¡ããããã®ã¹ã¯ãªãããäœæããŠããããŸãã¯ã«ãŒãããŒããUIãã«æ°èŠã§ã¹ã¯ãªãããã¢ã¿ããããããã®ãšãã¹ã¯ãªããã®ãã¡ã€ã«åã¯ãUI.gdããšããŠãããã¹ã¯ãªãããã¢ã¿ããããŠãã¹ã¯ãªãããšãã£ã¿ãéãããã以äžã®ããã«ã³ãŒããç·šéããã
extends Control
# å®æ°ãšããŠããªããŒããããµãŠã³ããªãœãŒã¹ãé
åã«ããŠãã
const sounds = [
# ããã©ã«ãã®ãªãœãŒã¹ã1çªç®ã®èŠçŽ ã«ããŠãã
preload("res://auido/coin.wav"),
preload("res://auido/heal.wav"),
preload("res://auido/hurt.wav"),
preload("res://auido/shoot1.wav"),
preload("res://auido/shoot2.wav"),
preload("res://auido/shoot3.wav"),
]
# é
å sounds ã®èŠçŽ ã®ãã¡ AudioStreamPlayer ã«èšå®ãã
# ãªãœãŒã¹ãæå®ããããã®ã€ã³ããã¯ã¹(0 ãã 5 ãŸã§)
var index: int = 0
# SoundLabel ã®åç
§
onready var sound_label = $SoundLabel
# AudioStreamPlayer ã®åç
§
onready var player = $AudioStreamPlayer
# ããŒããèªã¿èŸŒãŸãããå®è¡ãããçµèŸŒã¿é¢æ°
func _ready():
# ãµãŠã³ããåçããã¡ãœãããåŒã³åºãïŒããšã§å®çŸ©ïŒ
play_sound()
# çµã¿èŸŒã¿ããã»ã¹é¢æ°ïŒæ¯ãã¬ãŒã åŒã³åºãããïŒ
func _process(_delta):
# å³ç¢å°ããŒãæŒãããã
if Input.is_action_just_pressed("ui_right"):
# çŸåšã®ã€ã³ããã¯ã¹ãé
å sounds ã®æåŸã®ã€ã³ããã¯ã¹(5)
# ããå°ãããã°çŸåšã®ã€ã³ããã¯ã¹ã« 1 ã足ã
if index < sounds.size() - 1:
index += 1
# çŸåšã®ã€ã³ããã¯ã¹ãé
å sounds ã®æåŸã®ã€ã³ããã¯ã¹(5)
# ã§ããã°ã€ã³ããã¯ã¹ã 0 ã«æ»ã
else:
index = 0
# ãµãŠã³ããåçããã¡ãœãããåŒã³åºãïŒããšã§å®çŸ©ïŒ
play_sound()
# å·Šç¢å°ããŒãæŒãããã
if Input.is_action_just_pressed("ui_left"):
# çŸåšã®ã€ã³ããã¯ã¹ã 0 ãã倧ãããã°
# çŸåšã®ã€ã³ããã¯ã¹ãã 1 ãåŒã
if index > 0:
index -= 1
# çŸåšã®ã€ã³ããã¯ã¹ã 0 ã§ããã°
# ã€ã³ããã¯ã¹ãé
å sounds ã®æåŸã®ã€ã³ããã¯ã¹ãšåã(5)ã«ãã
else:
index = sounds.size() - 1
# ãµãŠã³ããåçããã¡ãœãããåŒã³åºãïŒããšã§å®çŸ©ïŒ
play_sound()
# ã¹ããŒã¹ããŒããšã³ã¿ãŒããŒãæŒãããã
if Input.is_action_just_pressed("ui_accept"):
# ãµãŠã³ããåçããã¡ãœãããåŒã³åºãïŒããšã§å®çŸ©ïŒ
play_sound()
# ãµãŠã³ããåçããã¡ãœãã
func play_sound():
# é
å sounds ããçŸåšã®ã€ã³ããã¯ã¹ã«è©²åœããèŠçŽ ãååŸããŠ
# AudioStreamPlayer ã® Stream ããããã£ã«é©çšãã
player.stream = sounds[index]
# AudioStreamPlayer ã® Stream ããããã£ã®ãªãœãŒã¹ãã¹ã
# SoundLabel ã® Text ããããã£ã« èšå®ãã
sound_label.text = player.stream.resource_path
# AudioStreamPlayer ã® Stream ããããã£ã®ãµãŠã³ããªãœãŒã¹ãåç
player.play()
ããã§ã¹ã¯ãªããã®ç·šéã¯å®äºã ã
ãªããä»åã¯preload
ãå©çšããŠãããŒããèªã¿èŸŒãŸããã¿ã€ãã³ã°ã§ãµãŠã³ããªãœãŒã¹ãèªã¿èŸŒãã§ããããã«ãããããã¯ãåŠçã®å€ãã²ãŒã ã®å Žåãåäœé
延åé¿çã«ãªãã ãããå察ã«ã倧ããŠåŠçãå€ããªãã®ã§ããã°load
ãå©çšããŠãåçããçŽåã«ãªãœãŒã¹ãã¡ã€ã«ãèªã¿èŸŒãã§ãåé¡ãªãã ããã詳ããã¯ä»¥äžã®èšäºã倧å€åèã«ãªãã®ã§äœµããŠã芧ããã ãããã
2dgames.jp -ãGodotããµãŠã³ããåçã«ããŒãããŠåçããæ¹æ³
åäœç¢ºèª
æåŸã«ãããžã§ã¯ããŸãã¯ã·ãŒã³ãå®è¡ããŠåäœç¢ºèªããŠããã
ãããŸã§ã§å®è£ ããéãã以äžã®ããŒããŒãæäœã§ãµãŠã³ããªãœãŒã¹ãåãæ¿ããåçã§ããã
- å³ç¢å°ããŒ: ãUI.gdãã¹ã¯ãªããã®é
å
sounds
äžã®æ¬¡ã®ãµãŠã³ããªãœãŒã¹ã«åãæ¿ããŠåç - å·Šç¢å°ããŒ: ãUI.gdãã¹ã¯ãªããã®é
å
sounds
äžã®åã®ãµãŠã³ããªãœãŒã¹ã«åãæ¿ããŠåç - ã¹ããŒã¹ã㌠/ ãšã³ã¿ãŒããŒ: çŸåšèšå®ãããŠãããµãŠã³ããªãœãŒã¹ãåç
ãããã«
ä»åã®ãã¥ãŒããªã¢ã«ã¯ãïŒã€ã®ãAudioStreamPlayerãããŒãã§è€æ°ã®ãµãŠã³ããªãœãŒã¹ãåãæ¿ããŠåçããæ¹æ³ã«ã€ããŠãäŒãããã倧äºãªãã€ã³ãããŸãšããŠãããã
- ãAudioStreamPlayerãïŒã€ã§è€æ°ã®ãµãŠã³ããªãœãŒã¹ãåãæ¿ããŠåçã§ããã
- äžèšææ³ã¯ãèšå®ãã»ãšãã©åãè€æ°ã®ãAudioStreamPlayerãããŒããè¿œå ããç¡é§ãåé¿ããã·ãŒã³ããªãŒã®èŠèªæ§ãåäžããç®çã§å®è£ ããã
- ãµãŠã³ãã®åãæ¿ããšåçã¯ã¹ã¯ãªããã§å¶åŸ¡ããã
ãã¡ãããããã°ã©ãã³ã°ãèŠæãªå Žåã¯ããµãŠã³ããªãœãŒã¹ã®æ°ã ããAudioStreamPlayerãããŒããã·ãŒã³ã«è¿œå ããŠãå šãåé¡ãªããä»å玹ä»ããã®ã¯ããããŸã§äžã€ã®æ¹æ³è«ã«éããªãã®ã§ãç¹ã«ã€ã³ãã£ãŒéçºã®å Žåã¯èªç±ã«ãã£ãŠããã ãããã
åè
- Godot Docs - ãªãŒãã£ãªã¹ããªãŒã
- Godot Docs - AudioStreamPlayer
- 2dgames.jp -ãGodotããµãŠã³ããåçã«ããŒãããŠåçããæ¹æ³