【发布时间】:2014-01-13 08:58:38
【问题描述】:
我正在使用时间处理程序在 Andengine 中生成精灵。
现在我想检测该精灵与同一场景中的其他精灵之间的碰撞。
我尝试使用下面的代码,但没有按预期工作。
还有其他检测碰撞的方法吗?
//method to create sprite
public void add()
{
mFaceCount++;
Random rand = new Random();
int x = (int) CAMERA_WIDTH + mb1.getWidth();
int minY = mb1.getHeight();
int maxY = (int) (CAMERA_HEIGHT/2 - mb1
.getHeight());
int rangeY = maxY + minY;
// int y = rand.nextInt(280) + 320;
int y=rand.nextInt(350-280) + 280;
int minDuration = 2;
int maxDuration = 3;
int rangeDuration = maxDuration - minDuration;
int actualDuration = rand.nextInt(rangeDuration) + minDuration;
if(this.mFaceCount%2==0)
{
targetSprite =new Sprite(x,y,mb4.clone());
mScene.attachChild(b1);
B1= PhysicsFactory.createBoxBody(this.mPhysicsWorld, b1, BodyType.KinematicBody, FIXTURE_DEF);
}
else if(this.mFaceCount%3==0)
{
targetSprite =new Sprite(x,y,mb2.clone());
mScene.attachChild(b1);
B1= PhysicsFactory.createBoxBody(this.mPhysicsWorld, b1, BodyType.KinematicBody, FIXTURE_DEF);
}
B1.setLinearVelocity(-5, 0);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(b1, B1, true, false)
{
@Override
public void onUpdate(float pSecondsElapsed)
{
super.onUpdate(pSecondsElapsed);
}
});
}
// time handler
private void createSpriteSpawnTimeHandler(){
TimerHandler spriteTimerHandler;
float mEffectSpawnDelay = 2f;
spriteTimerHandler = new TimerHandler(mEffectSpawnDelay,true,new ITimerCallback(){
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
add();
}
});
getEngine().registerUpdateHandler(spriteTimerHandler);
}
//update handler to detect collision
IUpdateHandler updateHandler = new IUpdateHandler()
{
@Override
public void reset() { }
@Override
public void onUpdate(final float pSecondsElapsed)
{
if (hanger.collidesWith(targetSprite))
{
System.out.println("Collision);
;
}
}
};
【问题讨论】:
-
"我试过但没用" 很好,您正在为解决问题付出一些努力,但我们需要看看您尝试了什么,因为我们无法告诉您其他是什么方式。
-
查看sscce.org 了解如何发布与您的问题相关的代码
-
thnks...但是你知道这个问题的解决方案吗?
-
不,抱歉。但至少我可以帮你得到更多更好的答案。
标签: android box2d andengine physics game-physics