问题
I have a set of sprite lists.i want to remove the topmost lying sprite if the sprites overlap.
if ( popRect1.containsPoint( popRect1, location ) ) {
BaloonList1.remove( baloons );
baloons.removeSelf();
}
if ( popRect2.containsPoint( popRect2, location ) ) {
BaloonList2.remove( baloons );
baloons.removeSelf();
}
When two sprites of same list overlap and if i touch them only the topmost sprite will be removed...if 2 sprites are from different list and when i touch the topmost sprite both of them are removing(Like my touch is intercepting..I think its happening because both the touch points are with the bounded rectangles of the sprites)..I searched alot.Some say override TouchBegan..registerWithTouchDispatcher...DOes any one have any idea ??????? i know this could be a easy question 4u guys.Please let me know if u want more code or description regarding this
回答1:
addChild(CCNode child, int z);
if you set the value of z like 0,1,2,3,4.. and so on ..
after this when you touch on the screen the value of z is decided to remove the sprite. what sprite you want to remove first in the case overlap is occur, give the value of z is max. to the others.
回答2:
You have to also to save the staus of which sprite is added firstly. Assuming we have 2 sprites A and B.
Case 1) if both A and B having same z values
If we add B sprite and after that A sprite then A will appear above the B sprite. So we need to set status of which sprite is added lastly. According to that you have to remove the sprite. Case 2) If A and B having different z values In this case. You have clear picture. The sprite which have more z value will appear above other. So you can remove that sprite.
In your case if you using case 1 then you have also to maintain th status.
Assuming we have two ArrayList: ArrayList1 and ArrayList2
and we are saving status as ArrayList: ArrayListStatus1,ArrayListStatus2
We have initialize the status to 1. int status=1;
If we add a sprite in any ArrayList we have also to add the in ArrayListStatus to corresponding status array list and have increment the status so next time when sprite will we added it will add one plus status.
Now we have the order so we can apply the check which sprite is above. The status is corresponding to there status arraylist.
来源:https://stackoverflow.com/questions/11305027/how-can-i-override-cctouchbegan-in-cocos2d-android-removal-of-overlaying-sprite