You can check the touch on sprite by calling default cocos methods
Firstly enable the touch(Cocos2D Touch HELP)
isTouchEnabled_=true;
after that in ccToucesBegan method you can check the touch on which sprite
(getBoundingBox() returns the CGRect)
@Override
public boolean ccTouchesBegan(MotionEvent event) {
CGPoint convertedLocation = CCDirector.sharedDirector()
.convertToGL(CGPoint.make(event.getX(), event.getY()));
if(sprite.getBoundingBox().contains(convertedLocation.x,convertedLocation.y))
{
removeChild(sprite, true);
}
}
As in your program you have not specified the sprite image here is the example layer class in which touch working properly
public class TestLayer extends CCColorLayer{
protected CCSprite target; protected CGPoint location;
public static CCScene scene(){
CCScene scene = CCScene.node();
CCColorLayer layer = new TestLayer(ccColor4B.ccc4(0, 0, 255, 255));
scene.addChild(layer);
return scene;
}
protected TestLayer(ccColor4B color){
super(color);
this.setIsTouchEnabled(true);
target=CCSprite.sprite("bee_120.png");
addChild(target);
target.setPosition(100, 100);
// this.schedule("game", 3.0f);
}
@Override
public boolean ccTouchesBegan(MotionEvent event) {
location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
if(CGRect.containsPoint(target.getBoundingBox(), location));
{
removeChild(target, true);
}
return true;
}
// public void game(float dt){
// addTarget();
// }
// protected void addTarget(){
// //some code of add the "target"
// }
}
In above code only one sprite used.. to use multiple sprite you have to schedule method as you have done but you have to check the touch for all sprite which you have generated. You can use ArrayList for that purpose.