大家好,我希望你能帮助我。我尝试编写游戏代码,但在碰撞中失败。我搜索了很多,发现边界框方法(在精灵周围创建一个不可见的矩形)对我来说是最好的解决方案。但是 intersect 方法对我不起作用。我有两个碰撞的位图精灵,但在 LogCat 中没有碰撞......
雪碧 1 级
public Sprite(GameView theGameView, Bitmap bmp) {
this.theGameView = theGameView;
this.bmp = bmp;
this.width = bmp.getWidth();
this.height = bmp.getHeight();
ySpeed = 0;
xSpeed = 1;
}
public Rect bounds() {
return (new Rect(x,y,width,height));
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bmp, x, y, null);
}
雪碧 2 级
public FourthSprite(GameView theGameView, Bitmap bmp) {
this.theGameView = theGameView;
this.bmp = bmp;
this.width = bmp.getWidth();
this.height = bmp.getHeight();
ySpeed = 0;
xSpeed = -1;
}
public Rect bounds() {
// TODO Auto-generated method stub
return (new Rect(x,y,width,height));
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bmp, x, y, null);
}
}
游戏视图类
公共无效碰撞(){
Rect r1 = theSprite.bounds(); // Sprite on left side
Rect r4 = theSprite4.bounds(); // Sprite on right side
if (r1.intersect(r4)){
collision = true;
Log.v("Log Tag", "COLLISION :D :D :D :D :D :D :D");
}
else {
collision = false;
Log.v("Log Tag", "NO COLLISION");
}
}
如果有帮助,我还可以上传视频。
编辑:http: //youtu.be/wYxZ7nKsmdw 我发现,当一个精灵不移动并且x,y坐标为0时,碰撞正在起作用。可能是什么问题?