我正在开发Android游戏,我正在尝试做的是一种墨水球游戏。
我解释说,我在这个网格中添加了一个网格和3个精灵。我最后想要的是,当你点击网格中的一个案例时,这个案例会变暗,这意味着当它们碰到这个“完整”案例之一的边界时,精灵会反弹并朝相反方向前进。然后你点击所有的情况,最后精灵都被困在同一个和最后一个白色的情况下,你点击它然后擦除3个精灵,你就赢得了游戏的等级。
目前我的网格和精灵都在这里,但当我点击一个案例时,精灵不会照顾它并继续朝着同一方向它们不会弹跳......这是我第一件事请求帮助,因为你在我的代码中我尝试了两种方法来添加boucing但是我想念一些......
第二个请求我的精灵在触摸屏幕的左上角和右上角时正在构成,但是在屏幕边框之前我的网格完成了底部所以我想添加一些东西让我的精灵在网格的底部边框上反弹。
请先在这里找到我的GameView代码:
public class GameView extends SurfaceView {
private Bitmap robotSprite;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
// Robot Sprite
private int x = 30;
private int y = 30;
private int xSpeed = 1;
private int top = 10;
private int left = 10;
private int right = 460;
private int bottom = 460;
int[][] grid;
Paint paint;
Sprite sprite;
private List<Sprite> sprites = new ArrayList<Sprite>();
public GameView(Context context)
{
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback()
{
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry)
{
try
{
gameLoopThread.join();
retry = false;
}
catch (InterruptedException e)
{
}
} // while (retry)
} // public void surfaceDestroyed(SurfaceHolder holder)
@Override
public void surfaceCreated(SurfaceHolder holder)
{
gameLoopThread.setRunning(true);
gameLoopThread.start();
} // public void surfaceCreated(SurfaceHolder holder)
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
});
paint = new Paint();
robotSprite = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
grid = new int[10][10];
for (int a = 0; a < 9; a++)
{
for (int b = 0; b < 9; b++)
{
grid[a][b] = 0;
}
}
robotSprite = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//sprite = new Sprite(this, robotSprite, 30, 30);
sprites.add( new Sprite(this, robotSprite, 30, 30) );
sprites.add( new Sprite(this, robotSprite, 130, 30) );
sprites.add( new Sprite(this, robotSprite, 230, 30) );
} // public GameView(Context context)
@Override
protected void onDraw(Canvas canvas)
{
try
{
canvas.drawColor(Color.WHITE); // White BackGround (I changed it)
int x = 10; //Decale les cases sur le cote droit
int y = 10;
paint.setStrokeWidth(3);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE); // Draw Border - Do NOT Fill!
//sprite.onDraw(canvas);
for (int rows = 0; rows < 9; rows++)
{
for (int line = 0; line < 9; line++)
{
if (grid[line][rows] == 0)
paint.setStyle(Paint.Style.STROKE); // Draw Border - Do NOT Fill!
else
paint.setStyle(Paint.Style.FILL_AND_STROKE); // Draw Border and FILL
canvas.drawRect(x, y, x+50, y+50, paint );
x = x + 50;
} // for (int line = 0; line < 9; line++)
x = 10;
y = y + 50;
} // for (int rows =0; rows < 9; rows++)
for (Sprite sprite : sprites)
{
sprite.Draw(canvas);
}
}
catch (Exception e)
{
Log.w("GameView","Exception = " + e.toString() );
}
} // protected void onDraw(Canvas canvas)
@Override
public boolean onTouchEvent(MotionEvent event)
{
int x = (int)event.getX()/50;
int y = (int)event.getY()/50;
try
{
if (grid[x][y] == 1)
grid[x][y] = 0;
else
grid[x][y] = 1;
}
catch (Exception e)
{
Log.w("GameView","TOUCH Exception = " + e.toString() );
}
Log.w("TOUCH","X = " + (int)event.getY()/50 + " Y = " + (int)event.getX()/50);
for (int i = sprites.size()-1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
if (sprite.isCollition(event.getX(),event.getY())) {
sprites.remove(sprite);
}
}
for (int i = sprites.size()-1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
if (sprite.isOutofBounds(event.getX(),event.getY()) == true) {
// change direction
sprite.ChangeDirection();
}
}
return super.onTouchEvent(event);
}
这里是我的Sprite.java代码,其中我尝试了两种方法(isOutofBounds和ChangeDirection,请参阅更新和更新2):
public Sprite(GameView gameView, Bitmap bmp, int newX, int newY)
{
this.gameView = gameView;
this.bmp = bmp;
this.x = newX;
this.y = newY;
this.width = bmp.getWidth();
this.height = bmp.getHeight();
} // public Sprite(GameView gameView, Bitmap bmp)
private void update()
{
if (x > gameView.getWidth() - width - xSpeed || x + xSpeed < 0)
{
xSpeed = -xSpeed;
}
x = x + xSpeed;
if (y > gameView.getHeight() - height - ySpeed || y + ySpeed < 0)
{
ySpeed = -ySpeed;
}
y = y + ySpeed;
} // private void update()
private void update2()
{
if (isOutofBounds(x, y) == true)
{
xSpeed = -xSpeed;
ySpeed = -ySpeed;
}
}
public void ChangeDirection()
{
update2();
}
public void Draw(Canvas canvas)
{
update();
canvas.drawBitmap(bmp, x, y, null);
}
public boolean isCollition(float x2, float y2) {
return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
}
public boolean isOutofBounds(float x2, float y2){
// Y
int top = 10;
int bottom = 460;
// X
int left = 10;
int right = 460;
if (y2 < top | y2 > bottom)
// outside bounds
return true;
if(x2 < left | x2 > right)
// outside bounds
return true;
return false;
}
} // public class Sprite {
希望你能帮助我。