Jac*_*sty 2 iphone objective-c cocos2d-iphone ios
我有这个精灵,我试图检测用户是否触摸它并发布NSLOG.我已经阅读了一些关于在stackoverflow上检测精灵触摸的cocos2d帖子,但我有点困惑并且不太明白.任何帮助将不胜感激.我将在下面发布我的精灵.
chew = [CCSprite spriteWithFile:@"chew.png" rect:CGRectMake(0, 0, 152, 152)];
chew.position = ccp(100, 300);
[self addChild:chew];
Run Code Online (Sandbox Code Playgroud)
弄清楚了
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint( [ chew boundingBox], location))
{
NSLog(@"touched");
}
}
Run Code Online (Sandbox Code Playgroud)
为您的精神提供标记值,并在触摸事件中比较该标记值
chew = [CCSprite spriteWithFile:@"chew.png" rect:CGRectMake(0, 0, 152, 152)];
chew.position = ccp(100, 300);
chew.tag=12;
[self addChild:chew];
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertTouchToNodeSpace: touch];
for (CCSprite *station in _objectList)
{
if (CGRectContainsPoint(station.boundingBox, location))
{
if(station.tag==12)
{
DLog(@"Found Your sprite");
return YES;
}
}
}
return NO;
}
Run Code Online (Sandbox Code Playgroud)