0
所以这就是发生了什么,游戏一直停止,因为两个圆圈相互碰撞,我试图通过确保ID是方形ID来防止这种情况,即时尝试让圆圈碰撞在一起只是忽略了碰撞,但没有做到这一点,我不知道如何阻止碰撞,只允许它与广场相撞。与精灵碰撞
基本上我想阻止圆与另一个圆碰撞,并允许它与广场相撞。
任何帮助将是巨大的谢谢
game.engine.engine类
//collision detection class
iter = p_group.listIterator();
Sprite spr = null;
while (iter.hasNext()) {
spr = (Sprite)iter.next();
//remove from list if flagged
if (!spr.getAlive()) {
iter.remove();
continue;
}
//is collision enabled for this sprite?
if (spr.getCollidable()) {
//has this sprite collided with anything?
if (spr.getCollided()) {
//is the target a valid object?
if (spr.getOffender() != null) {
/*
* External func call: notify game of collision
* (with validated offender)
*/
collision(spr);
//reset offender
spr.setOffender(null);
}
//reset collided state
spr.setCollided(false);
}
}
}
这是我为您在延伸game.engine.engine包中的游戏类碰撞。
游戏类
// all the circles were are set up the same, theres four
// circletopleft, circletopright, circlebottomleft
// circle bottom left collides with circle top right
// theres no diffence between the code of the circles they are all the same
/*ball*/circleBottomLeft.setCollidable(true);
circleBottomLeft.setIdentifier(CIRCLEID_3);
addToGroup(circleBottomLeft);
//init ball sprite
circleBottomRight.position = new Float2(550, h-550);
circleBottomRight.setVelocity(new Float2(10.0f, -9.0f));
//keep ball inside secreen boundary
Point size4 = circleBottomRight.getSize();
circleBottomRight.addAnimation(new ReboundBehavior(new RectF
(0 , 0 , w-size4.x, h-size4.y),size4,circleBottomRight.getVelocity()));
/*ball*/circleBottomRight.setCollidable(true);
circleBottomRight.setIdentifier(CIRCLEID_4);
addToGroup(circleBottomRight);
}
//collision class extended from game.engine.Engine
public void collision(Sprite sprite) {
// TODO Auto-generated method stub
final int oneMin = 60;
final int twoMin = 120;
final int threeMin = 180;
final int fourMin = 240;
final int fiveMin = 300;
Sprite other = sprite.getOffender();
Float2 vel = sprite.getVelocity();
switch (sprite.getIdentifier()) {
case CIRCLEID_1:
vel = sprite.getVelocity();
other.position.y += 1;
case CIRCLEID_2:
vel = sprite.getVelocity();
other.position.y += 1;
case CIRCLEID_3:
vel = sprite.getVelocity();
other.position.y += 1;
case CIRCLEID_4:
vel = sprite.getVelocity();
other.position.y += 1;
break;
}
switch (other.getIdentifier()) {
case oneMin:
if (this.getElapsedTime() == oneMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case twoMin:
if (this.getElapsedTime() == twoMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case threeMin:
if (this.getElapsedTime() == threeMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case fourMin:
if (this.getElapsedTime() == fourMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case fiveMin:
if (this.getElapsedTime() == fiveMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case SQUARE_ID:
//make sure its not touched
if (other.getIdentifier() == SQUARE_ID &&
other.getIdentifier() == CIRCLEID_1 ||
other.getIdentifier() == CIRCLEID_2 ||
other.getIdentifier() == CIRCLEID_3 ||
other.getIdentifier() == CIRCLEID_4) {
stop();
Log.d("Game", "Collided, I collided with something");
if (p_paused) {
Message msg = handler.obtainMessage();
msg.arg1 = 1;
handler.sendMessage(msg);
它使停车时顶部的两个圆相撞(417秒进入游戏那里有一个碰撞,并且它不触及广场。它发生的确切时间圆左下方碰撞与调用,我是能够检查正确的碰撞圈右上方