MATLAB Answers

How do I play sound when a key is pressed

4 ビュー (過去 30 日間)
Michael Mauricio
Michael Mauricio 2016 年 12 月 6 日
コメント済み: Michael Mauricio 2016 年 12 月 7 日
I am creating a game in matlab, how do I modify my code to play a sound when ever my character shoots (shoot key is pressed).
This is what I have so far, a working gun.
%shoot bullet
if(gamepad.isPressed(2)&& trigger_state == 0)
trigger_state=1;
bullet_speed = 2500;
end
if(gamepad.isPressed(2)== 0)
trigger_state=0;
end
moveXby(bullet, bullet_speed*frame_rate)
if (bullet_speed ==0)
moveYto(bullet,mean(guy.YData));
moveXto(bullet,mean(guy.XData));
end

採用された回答

Kirby Fears
Kirby Fears 2016 年 12 月 6 日
編集済み: Kirby Fears 2016 年 12 月 6 日
Michael,
This article describes how to record and play sound.
https://www.mathworks.com/help/matlab/import_export/record-and-play-audio.html
If you have a specific sound file that you need to play, try searching for that file type on this forum. However, I imagine it will be lots of fun recording your own sound effect as described in the link above.
Hope this helps.
  1 件のコメント
Michael Mauricio
Michael Mauricio 2016 年 12 月 7 日
Thank you for the help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by