views:

190

answers:

2

Hey Guys! Thanks so much for reading!

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];

    CGPoint location = [[CCDirector sharedDirector] convertToGL: [touch locationInView:touch.view]];
    CGRect myRect = CGRectMake(100, 120, 75, 113);

    int tjx = sprite.position.x;

    if(CGRectContainsPoint(myRect, location)) {
       tjx ++;            
    }
}

For some reason, ccTouchesEnded isn't allowing me to access my "sprite". I also tried to use CGRectMake like so :

CGRectMake( sprite.position.x, sprite.position.y, sprite.contentSize.Width, sprite.contentSize.Height) 

But I couldn't access my sprites position or height. I keep getting "sprite" undeclared when it is declared in the init method, and added to the child.

Please help!! I'm sure i'm missing something really simple here.

A: 

Have you tried...

  • [self sprite]
  • self.sprite
  • Checked if sprite is declared as a property and have you synthesized it?
Rob Segal
I used the Ktag method, and that fixed this issue. Thank you so much Rob!
maiko
A: 

"sprite" is probably declared locally in init method but not a member of the class.

One solution would be to give sprite a tag:

sprite.tag = 123; // any arbitrary number to identify this sprite

Later on you can access that sprite by using:

CCSprite* sprite = [self getChildByTag:123];

It is similar to removing a child by tag: http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14824-how-to-remove-a-child-from-the-nodescenelayer

GamingHorror
Thank you GH! tag'n fixed my issue
maiko