我在屏幕上以编程方式绘制了几个圆圈。然后我计算出手指点击的 x 和 y 坐标与圆的 x 和 y 坐标之间的距离。
哪个距离小于任何圆的半径就是被点击的圆。真的很简单。但是我发现我有很多重复的代码,我觉得我可以清理代码,但我不确定目前最好的方法是什么。
任何帮助表示赞赏。
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 方法中