4

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.

jnicz
  • 43
  • 4

1 Answers1

2

Try this:

CCSprite *temp = [CCSprite spriteWithFile:@"taco.png"];

temp = [birdLayer getChildByTag:100];

if (temp.position.x == location.x) {

    // do stuff...
}
Imirak
  • 1,323
  • 11
  • 21
neowinston
  • 7,584
  • 10
  • 52
  • 83
  • The problem is that the position of the parrot sprite doesn't update. so parrot.position.x remains at the initial location. – jnicz Jul 01 '12 at 04:26
  • Try this TouchableSprites Class. It works fine for me with the SetCanTrack:YES. http://www.cocos2d-iphone.org/forum/topic/5971 – neowinston Jul 01 '12 at 04:30
  • Actually the sprite doesn't move because in the iteration the last sprite will always be the one to be called, no matter the tag. – neowinston Jul 01 '12 at 04:44
  • The sprite moves, as in, the .png moves on the assigned layer. What do you mean? – jnicz Jul 01 '12 at 05:10
  • I've experienced in iterations that the redBirds sprite number 7 will be the one to be detected by touch, and receive the [self movement] call (because it's the last one in the CCArray) and not the the number 4, as you would like to. That's the reason the TouchableSprites are a nice way to handle touch. – neowinston Jul 01 '12 at 05:28