今日は最後『効果音をつける』です
まず下準備としてProjectタブのresにrawフォルダーを作成します
rawに音源をドラッグ&ドロップしておきます
今回は3つの音源を準備しました
音源は効果音ラボさんです
ありがとうございます
soundeffect-lab.info
import android.graphics.Color import android.media.AudioAttributes import android.media.SoundPool import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.view.animation.AlphaAnimation import android.view.animation.Animation import android.widget.Button import android.widget.TextView import android.widget.Toast private var textData =ArrayList<Int>() //画面上の数値データ private lateinit var sp: SoundPool //サウンドプール private var snd =0 //移動音 private var snd_shuffle = 0 //シャッフル音 private var snd_gameclear = 0 //ゲームクリア音 private var flag_clear = false //ゲームクリアフラグ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //効果音 val aa= AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME).setContentType( AudioAttributes.CONTENT_TYPE_SPEECH).build() sp= SoundPool.Builder().setAudioAttributes(aa).setMaxStreams(3).build() snd= sp.load(this,R.raw.snd,1) snd_shuffle= sp.load(this,R.raw.shuffle,1) snd_gameclear= sp.load(this,R.raw.gameclear,1) val text0: TextView = findViewById(R.id.text0) val text1: TextView = findViewById(R.id.text1) val text2: TextView = findViewById(R.id.text2) val text3: TextView = findViewById(R.id.text3) val text4: TextView = findViewById(R.id.text4) val text5: TextView = findViewById(R.id.text5) val text6: TextView = findViewById(R.id.text6) val text7: TextView = findViewById(R.id.text7) val text8: TextView = findViewById(R.id.text8) val text9: TextView = findViewById(R.id.text9) val text10: TextView = findViewById(R.id.text10) val text11: TextView = findViewById(R.id.text11) val text12: TextView = findViewById(R.id.text12) val text13: TextView = findViewById(R.id.text13) val text14: TextView = findViewById(R.id.text14) val text15: TextView = findViewById(R.id.text15) val text16: TextView = findViewById(R.id.text16) val text17: TextView = findViewById(R.id.text17) val text18: TextView = findViewById(R.id.text18) val text19: TextView = findViewById(R.id.text19) val text20: TextView = findViewById(R.id.text20) val text21: TextView = findViewById(R.id.text21) val text22: TextView = findViewById(R.id.text22) val text23: TextView = findViewById(R.id.text23) val text24: TextView = findViewById(R.id.text24) val textView = arrayOf( text0, text1,text2,text3,text4,text5,text6,text7, text8,text9,text10,text11,text12,text13,text14,text15, text16,text17,text18,text19,text20,text21,text22,text23,text24) val textmsg1: TextView = findViewById(R.id.textmsg1) textmsg1.setVisibility(View.INVISIBLE) //クリアメッセージ非表示 val shuffle: Button = findViewById(R.id.shuffle) val btback: Button = findViewById(R.id.btback) val dispData = arrayOf( " ","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20", "21","22","23","24") val list = listOf( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,17,18,19,20,21,22,23,24) var check=false //不可能配列かどうか(true:可能 false:不可能) while(check==false){ textData = list.shuffled() as ArrayList<Int> check = GoalCheck() } //シャッフルした数字を表示し0を空白にする for (i in 0..24) { textView[i].text = dispData[textData[i]] if(textData[i]==0){ textView[i].setBackgroundColor(Color.WHITE) } } //シャッフルボタンクリック shuffle.setOnClickListener{ sp.play(snd_shuffle, 1.0f, 1.0f, 1, 0, 1.0f) for (i in 0..24) { if(textData[i]==0){ //空白を紺に戻す textView[i].setBackgroundResource(R.color.puzzle5) break } } if(flag_clear){ for (i in 0..24) { textView[i].setClickable(true) //クリック可能にする textView[i].setBackgroundResource(R.color.puzzle5) //背景色を紺にする textmsg1.setVisibility(View.INVISIBLE) //クリアメッセージ非表示 } flag_clear=false } check=false while(check==false){ textData = list.shuffled() as ArrayList<Int> check = GoalCheck() } //シャッフルした数字を表示し0を空白にする for (i in 0..24) { textView[i].text = dispData[textData[i]] if(textData[i]==0){ textView[i].setBackgroundColor(Color.WHITE) } } } //戻るボタンクリック btback.setOnClickListener { sp.release() android.os.Process.killProcess(android.os.Process.myPid()) } } fun onNumberTap(view: View){ val text0: TextView = findViewById(R.id.text0) val text1: TextView = findViewById(R.id.text1) val text2: TextView = findViewById(R.id.text2) val text3: TextView = findViewById(R.id.text3) val text4: TextView = findViewById(R.id.text4) val text5: TextView = findViewById(R.id.text5) val text6: TextView = findViewById(R.id.text6) val text7: TextView = findViewById(R.id.text7) val text8: TextView = findViewById(R.id.text8) val text9: TextView = findViewById(R.id.text9) val text10: TextView = findViewById(R.id.text10) val text11: TextView = findViewById(R.id.text11) val text12: TextView = findViewById(R.id.text12) val text13: TextView = findViewById(R.id.text13) val text14: TextView = findViewById(R.id.text14) val text15: TextView = findViewById(R.id.text15) val text16: TextView = findViewById(R.id.text16) val text17: TextView = findViewById(R.id.text17) val text18: TextView = findViewById(R.id.text18) val text19: TextView = findViewById(R.id.text19) val text20: TextView = findViewById(R.id.text20) val text21: TextView = findViewById(R.id.text21) val text22: TextView = findViewById(R.id.text22) val text23: TextView = findViewById(R.id.text23) val text24: TextView = findViewById(R.id.text24) val textView = arrayOf( text0, text1,text2,text3,text4,text5,text6,text7, text8,text9,text10,text11,text12,text13,text14,text15, text16,text17,text18,text19,text20,text21,text22,text23,text24) val textmsg1: TextView = findViewById(R.id.textmsg1) val str = view.tag as String val tag = str.toInt() val baknumber = textData[tag] //スペースクリック if(textData[tag] == 0) Toast.makeText(this,"移動できません", Toast.LENGTH_SHORT).show() //右がスペース if(tag%5!=4 && textData[tag+1]==0){ textView[tag+1].setBackgroundResource(R.color.puzzle5) textView[tag+1].text = textData[tag].toString() textData[tag+1]=baknumber textView[tag].setBackgroundColor(Color.WHITE) textData[tag] = 0 sp.play(snd, 1.0f, 1.0f, 1, 0, 1.0f) } //左がスペース else if(tag%5!=0 && textData[tag-1]==0){ textView[tag-1].setBackgroundResource(R.color.puzzle5) textView[tag-1].text = textData[tag].toString() textData[tag-1]=baknumber textView[tag].setBackgroundColor(Color.WHITE) textData[tag] = 0 sp.play(snd, 1.0f, 1.0f, 1, 0, 1.0f) } //上がスペース else if(tag>4 && textData[tag-5]==0){ textView[tag-5].setBackgroundResource(R.color.puzzle5) textView[tag-5].text = textData[tag].toString() textData[tag-5]=baknumber textView[tag].setBackgroundColor(Color.WHITE) textData[tag] = 0 sp.play(snd, 1.0f, 1.0f, 1, 0, 1.0f) } //下がスペース else if(tag<20 && textData[tag+5]==0){ textView[tag+5].setBackgroundResource(R.color.puzzle5) textView[tag+5].text = textData[tag].toString() textData[tag+5]=baknumber textView[tag].setBackgroundColor(Color.WHITE) textData[tag] = 0 sp.play(snd, 1.0f, 1.0f, 1, 0, 1.0f) } else{ Toast.makeText(this,"移動できません", Toast.LENGTH_SHORT).show() } //クリアチェック var count=0 for (i in 0..23) { if(textData[i]!=i+1){ break } count++ } //ゲームクリア if(count == 24){ sp.play(snd_gameclear, 1.0f, 1.0f, 1, 0, 1.0f) flag_clear=true for (i in 0..23) { textView[i].setClickable(false) //クリアしたら押せなくなる textView[i].setBackgroundResource(R.color.puzzle5up) //半透明にする } textView[24].setBackgroundColor(Color.WHITE) textView[24].setClickable(false) textmsg1.setVisibility(View.VISIBLE) //クリアメッセージ表示 blinkText(textmsg1, 100, 100) } } //不可能配置のチェック fun GoalCheck():Boolean{ var num =IntArray(25) var zmove=true var nmove=true //空白が25の場所に移動するまでの最短距離が偶数か奇数か(1) var cnt=0 for(i in 0..24){ num[i] = textData[i] if(textData[i]==0) { num[i]=25 cnt = i } } val zx=(cnt%5)+1 val zy=(cnt/5)+1 if(((5-zx)+(5-zy))%2 == 0) zmove= true else zmove= false //正しい順に並べるまでの配置変換数が偶数か奇数か(2) cnt = 0 var bak=0 for(i in 0..24){ if(num[i]==i+1) continue for(j in i..24){ if(num[j]==i+1) { bak = num[i] num[i] = num[j] num[j]=bak cnt++ break } } } if(cnt%2==0) nmove=true else nmove=false //(1)と(2)が同じならtrue 違ったらfalse if(zmove == nmove) return true else return false } } //メッセージの点滅 private fun blinkText(txtView: TextView, duration: Long, offset: Long) { val anm: Animation = AlphaAnimation(0.0f, 1.0f) anm.duration = duration anm.startOffset = offset anm.repeatMode = Animation.REVERSE anm.repeatCount = 3 txtView.startAnimation(anm) }
SoundPoolを設定して音源をロード
音を鳴らしたい所で再生
ゲーム終了時にインスタンスを解放する
(再生できなくなっていたので2024.1.26に修正しました)
【注意】音でます
これで完成です