gpt4 book ai didi

android - 触摸时移除连续生成的 Sprite

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:28 25 4
gpt4 key购买 nike

我是 AndEngine 的新手。我刚刚创建了一个场景,其中包含许多在横向模式下从屏幕高度以上生成的 Sprite 。现在我想在触摸 Sprite 时将其移除。问题是,当我触摸屏幕时,最近的 Sprite 被移除,我无法单独移除点击的 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);

}

触摸时删除 Sprite 的代码:

// ==============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 创建新的 Sprite 。

我不确定我必须在哪里为特定的触摸 Sprite 编写代码并将其删除。

最佳答案

哦..我在这里找到了解决方案。

我在 BaseGameActivity 类中实现了一个 IOnAreaTouchListener。所以,我的类声明如下所示:

public class CrazyMonkeyActivity extends BaseGameActivity implements
IOnAreaTouchListener

我的覆盖方法如下所示:

@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final ITouchArea pTouchArea, final float pTouchAreaLocalX,
final float pTouchAreaLocalY) {

if (pSceneTouchEvent.isActionDown()) {
this.removeSprite((AnimatedSprite) pTouchArea);
return true;
}

return false;
}

移除 Sprite 的方法应该是这样的:

// ==============the method to remove spirit=====================
public void removeSprite(final AnimatedSprite target) {
runOnUpdateThread(new Runnable() {
@Override
public void run() {
mMainScene.detachChild(target);
mMainScene.unregisterTouchArea(target);
System.gc();
}
});
}

为了创建连续的 Spawn Sprite,我在此方法中使用了一个时间处理程序。应在 onLoadScene() 方法内部调用以下方法:

/** a Time Handler for spawning targets Sprites, triggers every 2 second */
private void createSpriteSpawnTimeHandler() {
TimerHandler spriteTimerHandler;
float mEffectSpawnDelay = 2f;

spriteTimerHandler = new TimerHandler(mEffectSpawnDelay, true,
new ITimerCallback() {
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
//position for random sprite
final float xPos = MathUtils.random(30.0f,
(cameraWidth - 30.0f));
final float yPos = MathUtils.random(30.0f,
(cameraHeight - 30.0f));
//below method call for new sprite
addTarget(xPos, yPos);

}

});
getEngine().registerUpdateHandler(spriteTimerHandler);
}

addTarget() 看起来像:

public void addTarget(float xPos, float yPos) {
Random rand = new Random();
Log.d("---Random x----", "Random x" + xPos + "Random y" + yPos);

target = new AnimatedSprite(xPos, yPos, mTargetTextureRegion);
target.animate(100);
mMainScene.attachChild(target);
mMainScene.registerTouchArea(target);

int minDuration = 2;
int maxDuration = 32;
int rangeDuration = maxDuration - minDuration;
int actualDuration = rand.nextInt(rangeDuration) + minDuration;

MoveYModifier mody = new MoveYModifier(actualDuration,
-target.getHeight(), cameraHeight + 10);
target.registerEntityModifier(mody.deepCopy());


}

希望这对其他人也有帮助!

关于android - 触摸时移除连续生成的 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13007822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com