我正在尝试制作游戏而我即将完成它。但问题是我的碰撞检测未被识别。得分只是不断增加。 他们告诉我把它移到LibGdx。但是考虑到我即将完成比赛,这将是一个麻烦。我只是担心如果我不使用库我的应用程序会崩溃。你们认为我应该转向LibGdx吗?
这是我的代码,仅用于碰撞检测部分..我的代码有问题吗?
我真的很难与碰撞检测。我将不胜感激。
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(final Animation animation) {
if(checkRedCollision() && checkRedCollision2() && checkBlueCollision() && checkBlueCollision2()
&& checkYellowCollision() && checkYellowCollision2() && checkGreenCollision2() && checkGreenCollision())
{
Score++;
}else {
Score--;
}
mhandle.post(new Runnable() {
@Override
public void run() {
mtvscore.setText("" + Score);
ballcolor=random.nextInt(4);
ballcolor2=random.nextInt(4);
ballcolor3=random.nextInt(4);
ball1.setImageResource(R.drawable.ball_0 + ballcolor);
ball2.setImageResource(R.drawable.row2_0 + ballcolor2);
ball3.setImageResource(R.drawable.row3_0 + ballcolor3);
// now we restart the animation
animation.setDuration(7500);
ball1.startAnimation(animation);
ball2.startAnimation(animation);
ball3.startAnimation(animation);
}
});
}
});
animation.setDuration(6500);
ball1.startAnimation(animation);
ball2.startAnimation(animation);
ball3.startAnimation(animation);
}
public boolean checkRedCollision() {
return checkCollisionR(ball1, rr, rg);
}
public boolean checkRedCollision2() {
return checkCollisionR2(ball1, ry, rb);
}
public boolean checkBlueCollision() {
return checkCollisionB(ball3, br, bg);
}
public boolean checkBlueCollision2() {
return checkCollisionB2(ball3, br, bg);
}
public boolean checkGreenCollision() {
return checkCollisionG(ball4, gr, gg);
}
public boolean checkGreenCollision2() {
return checkCollisionG2(ball4, gy, gb);
}
public boolean checkYellowCollision() {
return checkCollisionY(ball2, yr, yg);
}
public boolean checkYellowCollision2() {
return checkCollisionY2(ball2, yy, yb);
}
public boolean checkCollisionR(final View ball1, final View rr, final View rg) {
Rect R1 = new Rect(ball1.getLeft(), ball1.getTop(), ball1.getRight(), ball1.getBottom()); //red
Rect R2 = new Rect(rr.getLeft(), rr.getTop(), rr.getRight(), rr.getBottom());
Rect R3 = new Rect(rg.getLeft(), rg.getTop(), rg.getRight(), rg.getBottom());
return R1.setIntersect(R2, R3);
}
public boolean checkCollisionR2(final View ball1, final View ry, final View rb) {
Rect R1 = new Rect(ball1.getLeft(), ball1.getTop(), ball1.getRight(), ball1.getBottom()); //red
Rect R2 = new Rect(ry.getLeft(), ry.getTop(), ry.getRight(), ry.getBottom());
Rect R3 = new Rect(rb.getLeft(), rb.getTop(), rb.getRight(), rb.getBottom());
return R1.setIntersect(R2, R3);
}
public boolean checkCollisionY(final View ball2, final View yr, final View yg) { //
Rect R1 = new Rect(ball2.getLeft(), ball2.getTop(), ball2.getRight(), ball2.getBottom());
Rect R2 = new Rect(yr.getLeft(), yr.getTop(), yr.getRight(), yr.getBottom());
Rect R3 = new Rect(yg.getLeft(), yg.getTop(), yg.getRight(), yg.getBottom());
return R1.setIntersect(R2, R3);
}
public boolean checkCollisionY2(final View ball2, final View yy, final View yb) {
Rect R1 = new Rect(ball2.getLeft(), ball2.getTop(), ball2.getRight(), ball2.getBottom());
Rect R2 = new Rect(yy.getLeft(), yy.getTop(), yy.getRight(), yy.getBottom());
Rect R3 = new Rect(yb.getLeft(), yb.getTop(), yb.getRight(), yb.getBottom());
return R1.setIntersect(R2, R3);
}
public boolean checkCollisionB(final View ball3, final View br, final View bg) {
Rect R1 = new Rect(ball3.getLeft(), ball3.getTop(), ball3.getRight(), ball3.getBottom());
Rect R2 = new Rect(br.getLeft(), br.getTop(), br.getRight(), br.getBottom());
Rect R3 = new Rect(bg.getLeft(), bg.getTop(), bg.getRight(), bg.getBottom());
return R1.setIntersect(R2, R3);
}
public boolean checkCollisionB2(final View ball3, final View yy, final View yb) {
Rect R1 = new Rect(ball3.getLeft(), ball3.getTop(), ball3.getRight(), ball3.getBottom());
Rect R2 = new Rect(yy.getLeft(), yy.getTop(), yy.getRight(), yy.getBottom());
Rect R3 = new Rect(yb.getLeft(), yb.getTop(), yb.getRight(), yb.getBottom());
return R1.setIntersect(R2, R3);
}
public boolean checkCollisionG(final View ball4, final View gr, final View gg) {
Rect R1 = new Rect(ball4.getLeft(), ball4.getTop(), ball4.getRight(), ball4.getBottom());
Rect R2 = new Rect(gr.getLeft(), gr.getTop(), gr.getRight(), gr.getBottom());
Rect R3 = new Rect(gg.getLeft(), gg.getTop(), gg.getRight(), gg.getBottom());
return R1.setIntersect(R2, R3);
}
public boolean checkCollisionG2(final View ball4, final View gy, final View gb) {
Rect R1 = new Rect(ball4.getLeft(), ball4.getTop(), ball4.getRight(), ball4.getBottom());
Rect R2 = new Rect(gy.getLeft(), gy.getTop(), gy.getRight(), gy.getBottom());
Rect R3 = new Rect(gb.getLeft(), gb.getTop(), gb.getRight(), gb.getBottom());
return R1.setIntersect(R2, R3);
}
答案 0 :(得分:0)
至于LIBgdx,它看起来很有趣,但如果可以用Java完成,为什么会使进程复杂化。这是我们在一个名为Asteroids的非常基本的2D游戏中使用的一些代码。发射的船不会飞到屏幕上看起来太小而不能飞行。我们用JavaFX为桌面编写了相同的游戏,并使用了非常不同的碰撞检测。我们不是专业游戏开发者,所以这对某些人来说似乎很慢。图像视图粉红色在屏幕上移动的各种图像视图小行星上闪烁。尺寸在这里很重要,粉红色的子弹只有16乘16,小行星都是46乘48如果子弹的大小增加,就像是在近距离使用真正的枪械。很难错过
public void onFIRE(View view){
if(btnStart.getVisibility()== View.VISIBLE){
startLabel.setText("No Firing Start Game");
return;
}
gunANGLE = ivTRI.getRotation();
if(gunANGLE == 0.0f){
ZY -= 200.0;
ZX += 0;
pink.setX(pink.getX()+ZX);
pink.setY(pink.getY()+ZY);
//System.out.println("$$$$$$$$$$ Y "+pink.getY());
//System.out.println("$$$$$$$$$$ X "+pink.getX());
hitCHECK();
ZX = 0;
ZY = 0;
if( pink.getY() < 40){
HITorMISS();
pink.setX(ivTRI.getX()+30);
pink.setY(ivTRI.getY()+60);
// rotate gun bring bullet back to breach
HITorMISS();
// loose point for missing
}
}
这里的射击过程就是碰撞检测
public void hitCHECK(){
Rect pinkRect = new Rect();
pink.getHitRect(pinkRect);
Rect iv1Rect = new Rect();
iv1.getHitRect(iv1Rect);
Rect iv2Rect = new Rect();
iv2.getHitRect(iv2Rect);
Rect iv3Rect = new Rect();
iv3.getHitRect(iv3Rect);
Rect iv4Rect = new Rect();
iv4.getHitRect(iv4Rect);
Rect iv5Rect = new Rect();
iv5.getHitRect(iv5Rect);
Rect iv6Rect = new Rect();
iv6.getHitRect(iv6Rect);
Rect earthRect = new Rect();
earth.getHitRect(earthRect);
Rect asteroidRect = new Rect();
asteroid.getHitRect(asteroidRect);
Rect arrowLeftRect = new Rect();
arrowLeft.getHitRect(arrowLeftRect);
Rect arrowRightRect = new Rect();
arrowRight.getHitRect(arrowRightRect);
if (Rect.intersects(pinkRect, arrowLeftRect)) {
arrowLeft.setVisibility(View.INVISIBLE);
arrowLeft.setX(-100);
arrowLeft.setY(-100);
arrowLeft.setEnabled(false);
}
if (Rect.intersects(pinkRect, arrowRightRect)) {
arrowRight.setVisibility(View.INVISIBLE);
arrowRight.setX(-100);
arrowRight.setY(-100);
}
if (Rect.intersects(pinkRect, iv1Rect)) {
iv1.setVisibility(View.INVISIBLE);
iv1.setX(-100);
iv1.setY(-100);
}
if (Rect.intersects(pinkRect, iv2Rect)) {
iv2.setVisibility(View.INVISIBLE);
iv2.setX(-100);
iv2.setY(-100);
}
if (Rect.intersects(pinkRect, iv3Rect)) {
iv3.setVisibility(View.INVISIBLE);
iv3.setX(-100);
iv3.setY(-100);
}
if (Rect.intersects(pinkRect, iv4Rect)) {
iv4.setVisibility(View.INVISIBLE);
iv4.setX(-100);
iv4.setY(-100);
}
if (Rect.intersects(pinkRect, iv5Rect)) {
iv5.setVisibility(View.INVISIBLE);
iv5.setX(-100);
iv5.setY(-100);
}
if (Rect.intersects(pinkRect, iv6Rect)) {
iv6.setVisibility(View.INVISIBLE);
iv6.setX(-100);
iv6.setY(-100);
}
if (Rect.intersects(pinkRect, asteroidRect)) {
count = count + 1;
if (count > 5) {
asteroid.setVisibility(View.INVISIBLE);
asteroid.setX(-90);
asteroid.setY(-180);
timerA.cancel();
timerA.purge();
} else {
asteroid.setVisibility(View.VISIBLE);
asteroid.setX(-90);
asteroid.setY(-180);
asteroid.setRotation(asteroidANGLE = -0.0f);
// These settings make the asteroid follow a new
// path it continues to come back and attack the earth
}
}
if (Rect.intersects(pinkRect, earthRect)) {
Intent intent = new Intent(getApplicationContext(), you_hit_earth.class);
intent.putExtra("MSG", 1);
startActivity(intent);
stopGAME(null);
}
if (Rect.intersects(asteroidRect, earthRect)) {
Intent intent = new Intent(getApplicationContext(), you_hit_earth.class);
intent.putExtra("MSG", 2);
startActivity(intent);
stopGAME(null);
}
if(iv1.getVisibility()==View.INVISIBLE && iv2.getVisibility()==View.INVISIBLE&&
iv3.getVisibility()==View.INVISIBLE&&iv4.getVisibility()==View.INVISIBLE&&
iv5.getVisibility()==View.INVISIBLE&&iv6.getVisibility()==View.INVISIBLE&&
arrowLeft.getVisibility()==View.INVISIBLE&&arrowRight.getVisibility()==View.INVISIBLE){
Long spentTime = System.currentTimeMillis()/1000 - startTime;
score = spentTime;
Intent intent = new Intent(getApplicationContext(), result.class);
intent.putExtra("SCORE", score);
startActivity(intent);
stopGAME(null);
}
}
这个游戏有四个线程运行一个小行星一个用于arrowLeft和arrowRing小行星一个用于地球图像视图和一个小行星追逐地球必须被击中超过5次以使其消失
答案 1 :(得分:0)
这可能会让您了解代码冲突无法解决的原因。我们试图只有当某些边相交时两个图像视图上下移动而另一个左右移动时才发生碰撞
public void onFIRE(View view){
ZX += 100;
ZY += 0;
arrowLeft.setX(ZX);
arrowLeft.setY(arrowLeft.getY()+ZY);
hitCHECK();
}
public void hitCHECK(){
Rect arrowLeftRect = new Rect();
arrowLeft.getHitRect(arrowLeftRect);
Rect arrowRightRect = new Rect();
arrowRight.getHitRect(arrowRightRect);
int Lbottom = arrowLeftRect.bottom;
int Ltop = arrowLeftRect.top;
int Lleft = arrowLeftRect.left;
int Lright = arrowLeftRect.right;
System.out.println("$$$$$$$$ Left bottom "+Lbottom+" Top "+Ltop+" Left
"+Lleft+" Right "+Lright);
int Rbottom = arrowRightRect.bottom;
int Rtop = arrowRightRect.top;
int Rleft = arrowRightRect.left;
int Rright = arrowRightRect.right;
System.out.println("$$$$$$$$ Right bottom "+Rbottom+" Top "+Rtop+" Left
"+Rleft+" Right "+Rright);
if(arrowLeftRect.right > arrowRightRect.right){
arrowLeft.setVisibility(View.INVISIBLE);
arrowLeft.setX(-100);
arrowLeft.setY(-100);
}