画面にプログラムで描かれたいくつかの円があります。次に、指のクリックのx座標とy座標の間の距離を、円のx座標とy座標のそれぞれで計算します。
円の半径のいずれよりも小さい距離は、クリックされた円です。とてもシンプルです。しかし、私はiveが多くの繰り返しコードを取得していることを発見し、コードをクリーンアップできると感じていますが、現時点でそれを行うための最良の方法がわかりません。
どんな助けでも大歓迎です。
float diffx = touch.x - bass.pos.x;
float diffy = touch.y - bass.pos.y;
float dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < bass.radius){
if(recordingInfo.isRecording){
//do some stuff related to this button unique
}
//play some sound
}
diffx = touch.x - treble.pos.x;
diffy = touch.y - treble.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < treble.radius){
if(recordingInfo.isRecording){
//do something related to this button
}
//play some sound
}
diffx = touch.x - hihat.pos.x;
diffy = touch.y - hihat.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < hihat.radius){
if(recordingInfo.isRecording){
//do shayt related to this button
}
//play this sound
}
diffx = touch.x - bassTwo.pos.x;
diffy = touch.y - bassTwo.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < bassTwo.radius){
if(recordingInfo.isRecording){
//do some crap regarding this indivudal button
}
//play another sound
}
diffx = touch.x - kick.pos.x;
diffy = touch.y - kick.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < kick.radius){
if(recordingInfo.isRecording){
//do some funky stuff related to this button
}
//play some sound
}
diffx = touch.x - snare.pos.x;
diffy = touch.y - snare.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < snare.radius){
if(recordingInfo.isRecording){
//
}
//play some sound
}
diffx = touch.x - recordButton.pos.x;
diffy = touch.y - recordButton.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
if(dist < recordButton.radius){
//and do some funky stuff audio visual styff gere
}
diffx = touch.x - play.pos.x;
diffy = touch.y - play.pos.y;
dist = sqrt(diffx*diffx + diffy*diffy);
//code execution if this circle button is hit
}
またはこれは大丈夫ですか?このコードをすべてtouchDownメソッドに配置します