:d 我已經分離和attachChild一個問題..pEntity'nameentity'已經有一個父級:'場景'。新父母: '場景'
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
java.lang.IllegalStateException:pEntity'Ghost'已經有一個父項:'Scene'。新的父母:'場景'! 在org.andengine.entity.Entity.assertEntityHasNoParent(Entity.java:1412) 在blablabla ....
但如果我評論:
//this.getEngine().registerUpdateHandler(spriteTimerHandler = new TimerHandler(5,true,Callback = new ITimerCallback(){
//// @Override
//// public void onTimePassed(TimerHandler pTimerHandler) {
都做工精細...
有人可以幫助我嗎? :)
PS:對不起,我真的不好英語C.C
每個實體只有一個父母和多個孩子。當實體C連接到實體P時,C的父代被設置爲P,並且C正由內部的AndEngine添加到P的子代列表中。從上面的代碼中,您可以看到一個名爲「Blinky」的實體,但您多次將它附加到場景中。也許要麼改變你的計時器到oneshot計時器,或者你需要一個Blinky池,並附上一個免費的Blinky來感知你的重複計時器。 –