[英]How can I prevent these ranges from overlapping in Java?
I am writing some code in java which checks through an array of 51 objects called beatArr
which are circles which move from right to left towards a player positioned at x:200px.我在 java 中编写了一些代码,它通过一个名为
beatArr
的 51 个对象的数组进行检查,这些对象是从右向左移动到位于 x:200px 的玩家的圆圈。 I want to make it so that the player is awarded points or deducted points based on the timing of their button presses compared to the position of a beat, and so far I have the following:我想让玩家根据他们按下按钮的时间与节拍的 position 相比获得积分或扣除积分,到目前为止我有以下内容:
public static void hit(){
//Method for player to hit the beat
for (Beat beat : Game.beatArr) {
if (beat.getX() <= 220 && beat.getX() >=150){
score+=100;
System.out.println("Perfect+ "+score);
}
else if (beat.getX() <= 250 && beat.getX() >=100) {
score += 100;
System.out.println("Good "+score);
}
else if(beat.getX() <=99 && beat.getX()>=0 | beat.getX() >250){
score -=50;
System.out.println("Miss " + score);
}
}
This method causes several problems as the ranges overlap and so on a button press something like this usually gets outputted:这种方法会导致几个问题,因为范围重叠等等,通常会输出类似这样的按钮按下:
Miss 3000
Good 3100
Good 3200
Whereas I want it to only check a single position at a time really such as the one closest to the player for example or an iterator for checking the object at a given position in the array which I am not sure about the syntax for.而我希望它一次只检查一个 position,例如最接近播放器的一个,或者一个迭代器,用于在给定的 position 处检查 object。对于数组中的语法,我不确定。 So far I've tried changing the order of the checks to see if that affected anything which it unfortunately did not.
到目前为止,我已经尝试更改检查的顺序,看看这是否影响了它不幸没有影响的任何事情。
Testing out replacing beat.getX()
with Game.beatArr[counter].getX()
Where counter = 0 for now.测试用
Game.beatArr[counter].getX()
替换beat.getX()
现在 counter = 0。
It seems to check the position of the 1 beat 51 times, the full length of the array for some reason.似乎检查了 1 拍 51 次的 position,由于某种原因,数组的全长。 Output:
Output:
Good 100
Good 200
Good 300
Good 400
Good 500
Good 600
Good 700
Good 800
Good 900
Good 1000
Good 1100
Good 1200
Good 1300
Good 1400
Good 1500
Good 1600
Good 1700
Good 1800
Good 1900
Good 2000
Good 2100
Good 2200
Good 2300
Good 2400
Good 2500
Good 2600
Good 2700
Good 2800
Good 2900
Good 3000
Good 3100
Good 3200
Good 3300
Good 3400
Good 3500
Good 3600
Good 3700
Good 3800
Good 3900
Good 4000
Good 4100
Good 4200
Good 4300
Good 4400
Good 4500
Good 4600
Good 4700
Good 4800
Good 4900
Good 5000
Good 5100
Creation of beatArr: beatArr的创建:
public static Beat [] beatArr = new Beat [51];
int startingPoint = 800;
//For loop 51 times
for(int i=0;i<=50;i++){
//modifier to start position to create differing gaps between beats
int startModifier = random.nextInt(50);
// instantiates objects into an array for each iteration of the loop
handler.addObject(beatArr[i] = new Beat(startingPoint,300,ID.Beat));
//redefines the starting point for each beat
startingPoint = startingPoint+50+startModifier;
}
array of 51 objects called
beatArr
由 51 个对象组成的数组,称为
beatArr
I believe a queue would serve your purpose much better.我相信 队列会更好地满足您的目的。
public static void hit(){
//Method for player to hit the beat
if (Game.beatArr.size()>0) {
Beat beat = beatArr.remove();//use add() when inserting Beats into beatArr
if (beat.getX() <= 220 && beat.getX() >=150) {
score += 100;
System.out.println("Perfect+ " + score);
}
else if (beat.getX() <= 250 && beat.getX() >=100) {
score += 75;
System.out.println("Good " + score);
}
else if(beat.getX() <=99 && beat.getX()>=0 || beat.getX() >250){
score -= 50;
System.out.println("Miss " + score);
}
}
else {//No beats hence "Miss!"
score -= 50;
System.out.println("Miss " + score);
}
}
This function does the following:此 function 执行以下操作:
"Miss"
."Miss"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.