0

I need help I have tried to remove a ccnode that constantly is respawning at different locations and adding them to an array to get control of which sprites are on screen, but the thing is that I can't get to remove them. It detects the touches but doesn't get removed any ideas? Here is the code I'm using to get rid of the node.

 - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{

CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToUI:location];
for (CCNode *sprite in _spritesOnScreen) {
if (CGPointEqualToPoint(sprite.position, location)) {
    [_spritesOnScreen removeObject:sprite];
    [self removeChild:sprite cleanup:YES];

}
 }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    the chances of getting a point to match a point are very slim when you consider that the touch be some kind of geometric mean of all the points that are activated by a single fingertip. Best to check if the UITouch you are getting is in the bounding box of the apples. Many examples and questions on SO on that topic. Also, you are deleting from _appleOnScreen while iterating the array. That will bomb unless you iterate in reverse order. – YvesLeBorg Sep 24 '14 at 02:39

1 Answers1

0

Allow me to offer you a slightly different approach. Subclass CCNode to CCAppleNode and within the CCAppleNode.m file detect touches and call removeFromParent on touchBegan. This way the CCAppleNode class is taking up the responsibility of removing itself from parent when its touched, taking away this responsibility from your main game scene.

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    [self removeFromParentAndCleanup:YES];
    [super touchBegan:touch withEvent:event];
}
Abhineet Prasad
  • 1,271
  • 2
  • 11
  • 14
  • Thank you that worked perfectly, it solved the problem of removing the sprite from the scene but still, i need to implement a score system by adding points each time a sprite is touched and removed, the score system needs to be displayed in the main scene the thing is that that now the removing method its inside the CCAppleNode and the scoring system in the main scene what can i do to detect every time the removing method is called and modify the score ? – Diego Ramirez Sep 26 '14 at 22:30
  • There are many options for this.. one of the easiest even if a bit of an overkill is to use NSNotificationCenter where in you trigger an event in CCAppleNode and make the score managing system listen to that event. you'll find that this problem has been solved on 'so' multiple times before. – Abhineet Prasad Sep 27 '14 at 09:32
  • Hey. NSNotificationCenter was a great suggestion perfect for the code and you were right that problem has been solved many times ... sorry if im treating you like my developing guru but do you have any suggestion on how to detect if two objects collide, I have tried using ccPhysicsCollisionBegin with no success , need to detect if a CCAppleNode collides with a node i have created in SpriteBuilder as a static body i run the app and the sprite does collide with the node but it doesn't get detected as a collision. Do you have any suggestions? Thank You in Advance. – Diego Ramirez Sep 28 '14 at 23:05