我在 godot 工作,这是我的第一个项目,我决定制作一个钢琴游戏,就像那些虚拟钢琴游戏一样,你可以在键盘上弹奏,我一切正常,除了当我想演奏黑色音符时,它的转变+白色音符是它的父音符,例如我有 c2 作为 1 并且我想播放 c#2 它应该是 shift + 1 但它播放 c2
extends Node
# Define the note keymap for left and right hands
var left_hand = ['Z', 'S', 'X', 'D', 'C', 'V', 'G', 'B', 'H', 'N', 'J', 'M']
var right_hand = ['R', '5', 'T', '6', 'Y', 'U', '8', 'I', '9', 'O', '0', 'P']
# Define the piano notes
var piano_notes = ['C2', 'D2', 'E2', 'F2', 'G2','A2', 'B2', 'C3', 'D3', 'E3', 'F3', 'G3','A3', 'B3', 'C4',
'D4', 'E4', 'F4', 'G4','A4', 'B4', 'C5', 'D5', 'E5', 'F5', 'G5','A5', 'B5', 'C6', 'D6', 'E6', 'F6', 'F6#', 'G6', 'G6#',
'A6', 'B6', 'C7']
# Define white and black notes separately
var white_notes = ['C2', 'D2', 'E2', 'F2', 'G2',
'A2', 'B2', 'C3', 'D3', 'E3', 'F3', 'G3',
'A3', 'B3', 'C4', 'D4', 'E4', 'F4', 'G4',
'A4', 'B4', 'C5', 'D5', 'E5', 'F5', 'G5',
'A5', 'B5', 'C6', 'D6', 'E6', 'F6', 'G6',
'A6', 'B6', 'C7']
var black_notes = ['C#2', 'D#2', 'F#2', 'G#2',
'A#2', 'C#3', 'D#3', 'F#3', 'G#3',
'A#3', 'C#4', 'D#4', 'F#4', 'G#4',
'A#4', 'C#5', 'D#5', 'F#5', 'G#5',
'A#5', 'C#6', 'D#6', 'F#6', 'G#6',
'A#6', 'C#7']
# Combine white and black notes
var all_notes = white_notes + black_notes
# Dictionary to keep track of whether a key is pressed
var key_pressed = {}
func _ready():
# Initialize key_pressed dictionary
for note in piano_notes:
key_pressed[note] = false
# Connect input event
set_process_input(true)
func _process(delta):
# Check if a keybind is pressed for each note
for note in piano_notes:
var action = "play_note_" + note
if Input.is_action_pressed(action) and !key_pressed[note]:
play_note(note)
key_pressed[note] = true
elif !Input.is_action_pressed(action) and key_pressed[note]:
key_pressed[note] = false
func play_note(note):
# Load the audio file
var audio_stream = load("res://notes/" + note + ".wav")
if audio_stream:
# Create an AudioStreamPlayer node
var audio_player = AudioStreamPlayer.new()
# Assign the audio stream to the player
audio_player.stream = audio_stream
# Add the player to the scene
add_child(audio_player)
# Play the audio
audio_player.play()
我去为你们的所有代码添加了注释(或者至少要求聊天 gpt 解释所有内容并添加注释) 我的调试日志中还收到 4000 个错误:
E 0:00:02:0041 Piano.gd:45 @ _process(): The InputMap action "play_note_F6#" doesn't exist. Did you mean "play_note_F6"?
<C++ Error> Condition "!InputMap::get_singleton()->has_action(p_action)" is true. Returning: false
<C++ Source> core/input/input.cpp:288 @ is_action_pressed()
<Stack Trace> Piano.gd:45 @ _process()
每个键都这样
我尝试添加一个布尔变量来检查是否按下了shift,然后播放输入名称+“#”+注释,但这创建了一个双哈希标签
由于您仅在流程功能中检查
piano_notes
,因此仅检查您的主要注释。要检查是否按下了黑色音符操作,您需要使用 all_nodes
字典。
但是您应该已经注意到,如果除了预期输入之外还按下了其他按钮,则操作检查也是正确的。因此,假设如果按下 W 键,将会触发 play_note_C2 操作。那么如果用户按下 Shift+W,Input.is_action_pressed(“play_note_C2”) 也将为 true。
因此,如果您使用 all_nodes 字典,C2 和 C#2 将在输入处触发。
如果您仍然想通过定义的操作检查所有内容,则必须扩展白键的检查。因此:确保 Shift 未被按下:
func _process(delta):
# Check if a keybind is pressed for each white note
for note in white_notes:
var action = "play_note_" + note
if Input.is_action_pressed(action) and !key_pressed[note] and !Input.is_key_pressed(KEY_SHIFT):
play_note(note)
key_pressed[note] = true
elif !Input.is_action_pressed(action) and key_pressed[note]:
key_pressed[note] = false
#Now checking black notes:
for note in black_notes:
var action = "play_note_" + note
if Input.is_action_pressed(action) and !key_pressed[note]:
play_note(note)
key_pressed[note] = true
elif !Input.is_action_pressed(action) and key_pressed[note]:
key_pressed[note] = false
key_pressed 字典也必须正确初始化(因为它只是用 Piano_notes 初始化,它只保存你的白音符):
func _ready():
# Initialize key_pressed dictionary
for note in all_notes:
key_pressed[note] = false
# Connect input event
set_process_input(true)