5
我是AndEngine的新手。我只是在橫向模式下創建了一個場景,其中包含許多衍生自畫面高度的精靈。現在我想在觸摸它時刪除一個精靈。問題是,當我觸摸屏幕時,最近的精靈被刪除,我無法單獨刪除點擊的精靈。觸摸時移除連續的spawn sprite
這裏是我的代碼:添加目標
//======== TimerHandler for collision detection and cleaning up ======
IUpdateHandler detect = new IUpdateHandler() {
@Override
public void reset() {
}
@Override
public void onUpdate(float pSecondsElapsed) {
targets = targetLL.iterator();
while (targets.hasNext()) {
_target = targets.next();
if (_target.getY() >= cameraHeight) {
// removeSprite(_target, targets);
tPool.recyclePoolItem(_target);
targets.remove();
Log.d("ok", "---------Looop Inside-----");
// fail();
break;
}
// i don't know whether below code is valid for this postion
mMainScene.setOnSceneTouchListener(new IOnSceneTouchListener() {
@Override
public boolean onSceneTouchEvent(Scene arg0,
TouchEvent pSceneTouchEvent) {
try {
// i can't use this two
final float touchX = pSceneTouchEvent.getX();
final float touchY = pSceneTouchEvent.getY();
if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
//call to remove sprite
removeSprite(_target, targets);
}
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
});
}
targetLL.addAll(TargetsToBeAdded);
TargetsToBeAdded.clear();
}
};
代碼:感動時刪除精靈
/** adds a target at a random location and let it move along the y-axis */
public void addTarget() {
Random rand = new Random();
int minX = mTargetTextureRegion.getWidth();
int maxX = (int) (mCamera.getWidth() - mTargetTextureRegion.getWidth());
int rangeX = maxX - minX;
Log.d("----point----", "minX:" + minX + "maxX:" + maxX + "rangeX:"
+ rangeX);
int rX = rand.nextInt(rangeX) + minX;
int rY = (int) mCamera.getHeight() + mTargetTextureRegion.getHeight();
Log.d("---Random x----", "Random x" + rX + "Random y" + rY);
//HERE I USE POOL TO CREATE NEW SPRITE
//tPool is object of TargetsPool Class
target = tPool.obtainPoolItem();
target.setPosition(rX, rY);
target.animate(100);
mMainScene.attachChild(target, 1);
mMainScene.registerTouchArea(target);
int minDuration = 2;
int maxDuration = 32;
int rangeDuration = maxDuration - minDuration;
int actualDuration = rand.nextInt(rangeDuration) + minDuration;
// MoveXModifier mod = new MoveXModifier(actualDuration, target.getX(),
// -target.getWidth());
MoveYModifier mody = new MoveYModifier(actualDuration,
-target.getHeight(), cameraHeight + 10);
target.registerEntityModifier(mody.deepCopy());
TargetsToBeAdded.add(target);
}
代碼:
// ==============the method to remove sprite=====================
public void removeSprite(final AnimatedSprite _sprite, Iterator it) {
runOnUpdateThread(new Runnable() {
@Override
public void run() {
_target = _sprite;
mMainScene.detachChild(_target);
}
});
it.remove();
}
我用GenericPool創建新的精靈。
我不知道我必須爲特定的觸摸精靈編寫代碼並將其刪除。
@JhonEyer你在嗎? –