:D我有一个关于分离和attachChild的问题..
scene.registerUpdateHandler(new IUpdateHandler() {
@Override
public void reset() { }
@Override
public void onUpdate(final float pSecondsElapsed) {
// USELESS CODE
// USELESS CODE
// USELESS CODE
if (!SUPER){
if (player.collidesWith(Blinky) ||
player.collidesWith(Pinky) ||
player.collidesWith(Inky) ||
player.collidesWith(Clyde)){
player.animate(new long[]{130,130,130,130,130,130}, 24, 29, false);
player.stopPlayer();
}
}
else {
if (player.collidesWith(Blinky)){
scene.detachChild(Blinky);
respawnGhost("Blinky");
}
else if (player.collidesWith(Pinky)){
scene.detachChild(Pinky);
respawnGhost("Pinky");
}
else if (player.collidesWith(Inky)){
scene.detachChild(Inky);
respawnGhost("Inky");
}
else if (player.collidesWith(Clyde)){
scene.detachChild(Clyde);
respawnGhost("Clyde");
}
}
}
});有了这段代码,我分离了..
有了这个:
private void respawnGhost(final String ghost){
final Path RespawnPath = new Path(9).to(265, 295).to(225,295).to(225,255).to(305, 255).to(305, 295).to(225, 295).to(225,255).to(265,255).to(265,215);
TimerHandler spriteTimerHandler;
ITimerCallback Callback;
this.getEngine().registerUpdateHandler(spriteTimerHandler = new TimerHandler(5,true,Callback = new ITimerCallback(){
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
if(ghost.compareTo("Blinky") == 0){
scene.attachChild(Blinky);
Blinky.registerEntityModifier(new PathModifier(10, RespawnPath, null, new IPathModifierListener() {
@Override
public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
}
@Override
public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
}
@Override
public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
}
@Override
public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
Blinky.direction=4;
Blinky.ghostVel = (int)VELOCITY + 10;
Blinky.setVelocity((int)VELOCITY + 10,0);
Blinky.randDir = (int)(randRespawn.nextFloat() * 10)%3;
}
}));
}我重新连接..。
但是当调用OnTimePassed()时,TADAAAA!出现了异常:V
场景:场景‘pEntity’已经有一个父级:'Scene‘。新家长:“场景”!在布拉布拉市的org.andengine.entity.Entity.assertEntityHasNoParent(Entity.java:1412)……
但如果我评论:
//this.getEngine().registerUpdateHandler(spriteTimerHandler = new TimerHandler(5,true,Callback = new ITimerCallback(){
//// @Override
//// public void onTimePassed(TimerHandler pTimerHandler) {一切正常...
有人能帮我吗?:)
附言:很抱歉我的英语很差。
发布于 2013-06-22 23:35:55
我部分理解你的问题,并尽量给出最好的答案。首先,如果你想从场景中分离一些实体,那么你必须使用更新处理程序,就像下面的代码描述的那样。
mActivity.runOnUpdateThread(new Runnable(){
public void run(){
mScene.detachChild(mEntity);
}
});如果遇到任何异常,这是从场景中移除实体的首选方法。
您找到了IllegalStateException,因为您已经将该实体附加到场景中。因此,如果您尝试再次附加相同的实体,这将触发一个异常。
spriteTimerHandler = new TimerHandler(5,true,new ITimerCallback(){});我说了上面的语句,因为你在计时器处理程序中应用了true,所以它调用了多次。因此同一实体会一次又一次地尝试附加。
https://stackoverflow.com/questions/17251473
复制相似问题