android - AndEngine move two objects simultaneously -
i creating game using andengine , in want object flying in sky drop bombs below. using timehandler create both flying object , bombs. but not working properly. problems facing are:
1) instead of single bomb 2 or 3 bombs dropped.
2) , bomb objects not getting recycled. code recycling object getting called, doesn't seem work.
have noticed 1 thing i.e. suppose, have 1 flying object drops bomb. when bomb leaves screen, recycling code bomb object gets called once. , after sometime flying object leaves screen gets recycled. when flying object created , drops bomb, code recycling bomb getting called twice. third flying object, code recycling single bomb getting called thrice , on.
the code add flying object follows
private void createdragonhandler() { timerhandler spritetimerhandler; float meffectspawndelay = 10f; spritetimerhandler = new timerhandler(meffectspawndelay, true, new itimercallback() { @override public void ontimepassed(timerhandler ptimerhandler) { adddragon(); } }); getengine().registerupdatehandler(spritetimerhandler); } public void adddragon() { dragon dragon = (dragon) dragonspritepool.obtainpoolitem(); if (dragon.getparent() != mainscene) mainscene.attachchild(dragon); } public synchronized dragon obtainpoolitem() { dragon = super.obtainpoolitem(); random rand = new random(); final int x = (int) (camera_width + resourcemanager.dragontextureregion .getwidth()); int miny = (int) resourcemanager.dragontextureregion.getheight(); int maxy = (int) (camera_height / 2 - resourcemanager.dragontextureregion .getheight()); int rangey = maxy - miny; final int y = rand.nextint(rangey) + miny; dragon.reset(); dragon.setvisible(true); dragon.setignoreupdate(false); dragon.animate(150); dragon.setposition(x, y); movexmodifier mod = new movexmodifier(15, dragon.getx(), -dragon.getwidth()); float meffectspawndelay = 5f; timerhandler spritetimerhandler = new timerhandler(meffectspawndelay, true, new itimercallback() { @override public void ontimepassed(timerhandler ptimerhandler) { dragonfire dragonfire = birdshoot.dragonfirespritepool .obtainpoolitem(dragon.getx(), dragon.gety()); birdshoot.mainscene.attachchild(dragonfire); } }); dragon.registerentitymodifier(mod.deepcopy()); dragon.setnohit(0); physicshandler mphysicshandler; mphysicshandler = new physicshandler(dragon); dragon.setphysicshandler(mphysicshandler); dragon.registerupdatehandler(dragon.getmphysicshandler()); birdshoot.engine.registerupdatehandler(spritetimerhandler); return dragon; }
`
where writing code recycling sprite objects? according problem, see fishy there. might possible code not getting executed. code recycle sprites must this:-
public void onhandlerecycleitem(sprite sprite){ super.onhandlerecycleitem(sprite); // code recycle object }
also should called using following line :
yourpool.recyclepoolitem(spriteobject);
Comments
Post a Comment