Objective c 为触摸定义精灵形状
我正在尝试使用以下代码向NSArray中的精灵添加形状:Objective c 为触摸定义精灵形状,objective-c,cocos2d-iphone,shape,ccsprite,cgpath,Objective C,Cocos2d Iphone,Shape,Ccsprite,Cgpath,我正在尝试使用以下代码向NSArray中的精灵添加形状: theSprites = [[NSMutableArray alloc] init]; CCSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:@"sprite1"]; sprite1.position = ccp(winSize.width/2, sprite1.contentSize.height/2); [self addChild:sprite1 z:3]; sprite1
theSprites = [[NSMutableArray alloc] init];
CCSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:@"sprite1"];
sprite1.position = ccp(winSize.width/2, sprite1.contentSize.height/2);
[self addChild:sprite1 z:3];
sprite1Path=CGPathCreateMutable();
CGPathMoveToPoint(sprite1Path, NULL, 0, 286);
CGPathAddLineToPoint(sprite1Path, NULL, 0, 0);
CGPathAddLineToPoint(sprite1Path, NULL, 768, 0);
CGPathAddLineToPoint(sprite1Path, NULL, 768, 208);
CGPathAddLineToPoint(sprite1Path, NULL, 356, 258);
CGPathCloseSubpath(sprite1Path);
[theSprites addObject:sprite1];
CCSprite *sprite2 = [CCSprite spriteWithSpriteFrameName:@"sprite2"];
sprite2.position = ccp(winSize.width/2, sprite2.contentSize.height/2);
[self addChild:sprite2 z:4];
sprite2Path=CGPathCreateMutable();
CGPathMoveToPoint(sprite2Path, NULL, 0, 254);
CGPathAddLineToPoint(sprite2Path, NULL, 0, 0);
CGPathAddLineToPoint(sprite2Path, NULL, 768, 0);
CGPathAddLineToPoint(sprite2Path, NULL, 768, 144);
CGPathAddLineToPoint(sprite2Path, NULL, 494, 168);
CGPathAddLineToPoint(sprite2Path, NULL, 204, 212);
CGPathCloseSubpath(sprite2Path);
[theSprites addObject:sprite2];
然后,我试图指定只有这个定理是可论证的。我创建了一个类似于cocos2d教程中的函数
- (void)selectSprite:(CGPoint)touchLocation {
CCSprite *touchSprite = nil;
for (CCSprite *sprite in theSprites) {
if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
touchSprite = sprite;
} } }
现在我是斯塔克!
我不知道如何将CGRectContainsPoint更改为CGPathContainsPoint。。。
我不知道如何用一个语句指定两个形状。。。或者创建if()if()构造…我看到两个问题。首先,需要将路径附加到精灵。您应该子类化
CCSprite
,并给它一个boundingPath
属性:
MySprite.h
MySprite.m
然后,在创建精灵时设置shapePath
属性:
MySprite *sprite1 = [MySprite spriteWithSpriteFrameName:@"sprite1"];
// ... blah blah blah all the stuff you did to init sprite1 and sprite1Path
CGPathCloseSubpath(sprite1Path);
sprite1.shapePath = sprite1Path;
[theSprites addObject:sprite1];
现在,您可以在精灵中测试触摸,如下所示:
- (MySprite *)spriteAtPoint:(CGPoint)point {
for (MySprite *sprite in theSprites) {
CGPoint spriteOrigin = sprite.boundingBox.origin;
CGPoint pointInSprite = CGPointMake(point.x - spriteOrigin.x, point.y - spriteOrigin.y);
if (CGPathContainsPoint(sprite.shapePath, NULL, pointInSprite, false))
return sprite;
}
}
}
当我尝试添加
@property(非原子)CGPath shapePath时代码>有一个“必须使用'struct'标记来引用类型'CGPath'”。我需要添加“struct”吗?
MySprite *sprite1 = [MySprite spriteWithSpriteFrameName:@"sprite1"];
// ... blah blah blah all the stuff you did to init sprite1 and sprite1Path
CGPathCloseSubpath(sprite1Path);
sprite1.shapePath = sprite1Path;
[theSprites addObject:sprite1];
- (MySprite *)spriteAtPoint:(CGPoint)point {
for (MySprite *sprite in theSprites) {
CGPoint spriteOrigin = sprite.boundingBox.origin;
CGPoint pointInSprite = CGPointMake(point.x - spriteOrigin.x, point.y - spriteOrigin.y);
if (CGPathContainsPoint(sprite.shapePath, NULL, pointInSprite, false))
return sprite;
}
}
}