Cocos2D TouchesEnded not allowing me to access sprites?
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 hei开发者_StackOverflow社区ght. 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.
"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
Have you tried,
[self sprite]
self.sprite
- Checked if sprite is declared as a property and have you synthesized it?
精彩评论