Search code examples
cocoa-touchcocos2d-iphonecocos2d-androidcocos2d-x

Getting location of sprite within array cocos2d`


I need to be able to touch a specific moving sprite in my array and perform an action on it. However when I perform my MoveTo action, the sprite location doesn't update. Help!

Array:

int numbreds = 7;

redBirds = [[CCArray alloc] initWithCapacity: numbreds];

for( int i = 1; i<=numbreds; i++){

    int xvalue = ((-50*i) + 320);
    int yvalue= 160;


    if (i==4)
    { 
        CCSprite *parrot = [CCSprite spriteWithFile:@"taco.png"];

        [birdLayer addChild:parrot];
        [self movement]; //the action that moves the array horizontally
        parrot.position = ccp(xvalue,yvalue);
        parrot.tag=100;

Touch

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];


CCSprite *mark = (CCSprite *)[birdLayer getChildByTag:100];

if (CGRectContainsPoint([mark boundingBox], location))
{

    CCLOG(@"YAY!");
}

THe problem is that the location of the CCSprite doesn't actually update or move. YAY! only is generated at the origin location of the sprite.


Solution

  • Try this:

    CCSprite *temp = [CCSprite spriteWithFile:@"taco.png"];
    
    temp = [birdLayer getChildByTag:100];
    
    if (temp.position.x == location.x) {
    
        // do stuff...
    }