Reputation: 9606
How can I get a subclass of sprite to call a selector when touched?
I want a Sprite to react to touch and call a selector when touch is ended. I know how to get it to react to touches but I do not know how to specify which selector I should call.
Any help?
Upvotes: 0
Views: 133
Reputation: 8769
Do you mean you want to be able to set a target and selector on a sprite?
You can do that by setting up a method that stores a target and a selector in an instance variable.
__weak id _target;
SEL _selector;
-(void)setTarget:(id)target andSelector:(SEL)selector
{
_target = target;
_selector = selector;
}
-(void)ccTouchesEnded...
{
[_target performSelector:_selector];
}
Upvotes: 2
Reputation: 22042
In layer, first enable touch and add ccTouchesBegan to track touch.
self.isTouchEnabled = YES;
You can use this function to find touch.
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint touchLocation = [myTouch locationInView:[myTouch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:location];
if(CGRectContainsPoint([sprite boundingBox], touchLocation) )
{
[sprite youTouched];
}
}
Upvotes: 1