Removing random object from parent CCprite

Posted by Natty on Stack Overflow See other posts from Stack Overflow or by Natty
Published on 2014-06-02T15:12:39Z Indexed on 2014/06/02 15:26 UTC
Read the original article Hit count: 157

Filed under:

probably this is a simple problem to solve, since I'm quite new.

I have a scene with a waiter holding a tray of food
(the food is a random CCSprite choosen from an array)

each time he comes onscreen he holds a new piece of food
(the user touches the food and the waiter walks off to return
again with a new piece of food

however I cant seem to delete the old peice of food from the screen as it says the child is already added... any help would be great

 -(id) init
 {
    ///other code then...

    waiterOnSCreen
    = [CCSprite spriteWithSpriteFrameName:@"CatUP.png"];
    waiterOnSCreen.position = ccp(CatOffSCreenPosX, catXpos);
    [self addChild:waiterOnSCreen z:0];

    //moving the waiter
    // the random food sprite is added later to the waiter
    // [waiterOnSCreen addChild:myRandomSprite];
  }


-(void)LoadRandomFood
{
///I make my array here then...

    int i = arc4random() % [RandomFood count];
    myRandomSprite = (CCSprite *)[RandomFood  objectAtIndex:i];

    //waiterOnSCreen is a CCSprite added on the init
    [waiterOnSCreen addChild:myRandomSprite];
    myRandomSprite.position=ccp(290,220);

    myRandomSprite.tag = RandomFoodTag; 
    }
}

later

        in if(CGRectContainsPoint(waiterOnSCreen.boundingBox, location))
        {
         //trying to remove the food here

         //Ive already tried to remove the sprite using 
         [self removeChildByTag:RandomeObjectTag];

       //and also
        CCSprite *wantedSprite = (CCSprite *)[self getChildByTag:RandomFoodTag];
        [wantedSprite removeFromParentAndCleanup:YES];
        }

        }

© Stack Overflow or respective owner

Related posts about cocos2d-iphone