我在为java爆发游戏编码碰撞检测方面遇到了一些问题。我试图解决碰撞的方法就是这个:
public void runAsSeparateThread()
{
final float S = 3; // Units to move (Speed)
try
{
synchronized ( Model.class ) // Make thread safe
{
GameObj ball = getBall(); // Ball in game
GameObj bat = getBat(); // Bat
List<GameObj> bricks = getBricks(); // Bricks
}
while (runGame)
{
synchronized ( Model.class ) // Make thread safe
{
float x = ball.getX(); // Current x,y position
float y = ball.getY();
// Deal with possible edge of board hit
if (x >= W - B - BALL_SIZE) ball.changeDirectionX();
if (x <= 0 + B ) ball.changeDirectionX();
if (y >= H - B - BALL_SIZE) // Bottom
{
ball.changeDirectionY(); addToScore( HIT_BOTTOM );
}
if (y <= 0 + M ) ball.changeDirectionY();
// As only a hit on the bat/ball is detected it is
// assumed to be on the top or bottom of the object.
// A hit on the left or right of the object
// has an interesting affect
boolean hit = false;
if ( y <= bricks.getY() - (brickHeight/2)){
hit = true;
}
if ( y >= bricks.getY() - (brickHeight/2)){
hit = true;
}
if ( x < bricks.getX()){
hit = true;
}
if ( x > bricks.getX()){
hit = true;
}
if (hit)
ball.changeDirectionY();
if ( ball.hitBy(bat) )
ball.changeDirectionY();
}
modelChanged(); // Model changed refresh screen
Thread.sleep( fast ? 2 : 20 );
ball.moveX(S); ball.moveY(S);
}
} catch (Exception e)
{
Debug.error("Model.runAsSeparateThread - Error\n%s",
e.getMessage() );
}
}
然而,从我上面的方法来看,当我试图获得X&amp; Y来自砖块。它不断返回&#34;找不到符号 - 方法getY()&#34;。然而,这种方法似乎适用于蝙蝠和球,但只会为砖块抛出此错误。
此部分代码抛出此错误:
boolean hit = false;
if ( y <= bricks.getY() - (brickHeight/2)){
hit = true;
}
if ( y >= bricks.getY() - (brickHeight/2)){
hit = true;
}
if ( x < bricks.getX()){
hit = true;
}
if ( x > bricks.getX()){
hit = true;
}
如果有人可以提供帮助,我会非常感激!
更新
for ( int i = 0; i <= 60; i++ ){
GameObj brick1 = bricks.get(i);
if ( y <= brick1.getY() - (BRICK_HEIGHT/2)){
hit = true;
Debug.trace("BreakOut");
}
if ( y >= brick1.getY() - (BRICK_HEIGHT/2)){
hit = true;
Debug.trace("BreakOut");
}
if ( x < brick1.getX()){
hit = true;
Debug.trace("BreakOut");
}
if ( x > brick1.getX()){
hit = true;
Debug.trace("BreakOut");
}
}
答案 0 :(得分:2)
private List<GameObj> bricks; // The bricks
bricks
是许多砖块的清单。你想要哪一块砖?