我正在使用此方法在andengine中为我的场景添加一个新的精灵。
private void addBody(final float pX, final float pY) {
final Scene scene = this.mEngine.getScene();
if (BodyCount >= MAX_BODIES) return;
BodyCount++;
final Sprite matSprite;
final Body body;
if(BodyCount % 2 == 0) {
matSprite = new Sprite(pX, pY,
mTargetTextureRegion);
body = PhysicsFactory.createBoxBody(mPhysicsWorld,
matSprite, BodyType.DynamicBody,
FIXTURE_DEF);
} else {
matSprite = new Sprite(pX, pY,
mTargetTextureRegion);
body = PhysicsFactory.createCircleBody(
mPhysicsWorld, matSprite,
BodyType.DynamicBody, FIXTURE_DEF);
}
scene.getLastChild().attachChild(matSprite);
mPhysicsWorld.registerPhysicsConnector(
new PhysicsConnector(matSprite, body, true, true));
}
这里我调用方法向场景添加一个主体 但没有任何反应。我登录以查看代码是否正在运行并返回true。
private void createSpriteSpawnTimeHandler(){
TimerHandler spriteTimerHandler;
float mEffectSpawnDelay = 1f;
Random rand = new Random();
final float x = (int) mCamera.getWidth() + mTargetTextureRegion.getWidth();
int minY = mTargetTextureRegion.getHeight();
int maxY = (int)(mCamera.getHeight() - mTargetTextureRegion.getHeight());
int rangeY = maxY - minY;
final float y = rand.nextInt(rangeY) + minY;
spriteTimerHandler = new TimerHandler(mEffectSpawnDelay,true,new ITimerCallback(){
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
Log.e("MainGame", "Adding Body");
addBody(x,y);
}
});
getEngine().registerUpdateHandler(spriteTimerHandler);
}
问题是什么?或者最好的方法是什么? 我想要发生的是精灵被添加到屏幕的顶部并沿着屏幕流下,然后最终离开屏幕。