定义触摸的精灵形状

发布于 2024-12-29 12:01:51 字数 1634 浏览 1 评论 0原文

我正在尝试使用以下代码向 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];
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];

然后我尝试指定只有 theSprites 是可移动的。我创建了一个类似于 cocos2d 教程中的函数。

- (void)selectSprite:(CGPoint)touchLocation {
CCSprite *touchSprite = nil;
for (CCSprite *sprite in theSprites) {
  if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {            
    touchSprite = sprite;
    }  }  }

现在我已经堆栈了! 我不明白如何将 CGRectContainsPoint 更改为 CGPathContainsPoint... 我不知道如何用一个语句指定两种形状...或创建 if () if () 构造...

I'm trying to add shapes to my sprites, wich are in NSArray, using this code:

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];

Then i'm trying to specify that only theSprites are moovable. I created a function like in one of cocos2d tutorials.

- (void)selectSprite:(CGPoint)touchLocation {
CCSprite *touchSprite = nil;
for (CCSprite *sprite in theSprites) {
  if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {            
    touchSprite = sprite;
    }  }  }

And now i'm stack!
And i don't understand how to change CGRectContainsPoint to CGPathContainsPoint...
I don't know how to specify both shapes whith one statement... or create if () if () constraction...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2025-01-05 12:01:51

我看到两个问题。首先,您需要将路径附加到精灵上。您应该子类化CCSprite并给它一个boundingPath属性:

MySprite.h

@interface MySprite : CCSprite

@property (nonatomic) CGPath shapePath;

@end

MySprite.m

@implementation MySprite

@synthesize shapePath = _shapePath;

- (void)setShapePath:(CGPath)path {
    CGPathRetain(path);
    CGPathRelease(_shapePath);
    _shapePath = path;
}

- (void)dealloc {
    CGPathRelease(_shapePath);
    [super dealloc]; // if you're using ARC, omit this line
}

然后在创建精灵时设置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;
        }
    }
}

I see two problems. First, you need to attach the path to the sprite. You should subclass CCSprite and give it a boundingPath property:

MySprite.h

@interface MySprite : CCSprite

@property (nonatomic) CGPath shapePath;

@end

MySprite.m

@implementation MySprite

@synthesize shapePath = _shapePath;

- (void)setShapePath:(CGPath)path {
    CGPathRetain(path);
    CGPathRelease(_shapePath);
    _shapePath = path;
}

- (void)dealloc {
    CGPathRelease(_shapePath);
    [super dealloc]; // if you're using ARC, omit this line
}

You then set the shapePath property when you create the sprite:

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];

Now you can test for the touch in the sprite like this:

- (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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文