CGRectContainsPoint问题

时间:2012-02-19 21:56:15

标签: objective-c cocos2d-iphone touch

在我的测试游戏中,我有一些精灵( Bubbles = NSMutableArray),它们出现在屏幕底部的随机位置。

我有 addBubble spawBubble 方法:

- (void) addBubble {

CGSize winSize = [[CCDirector sharedDirector] winSize]; 
bubbles = [[NSMutableArray alloc] init];

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"bubbleSpriteList.plist"];
CCSpriteBatchNode *bubbleSpriteList = [CCSpriteBatchNode batchNodeWithFile:@"bubbleSpriteList.png"];
[self addChild:bubbleSpriteList];



bigBubble = [CCSprite spriteWithSpriteFrameName:@"bubble"];
[self addChild:bigBubble];
[bubbles addObject:bigBubble];


for (CCSprite *bubble in bubbles) {


    int minX = bubble.contentSize.width/2;
    int maxX = winSize.width-bubble.contentSize.width/2;
    int rangeX = maxX - minX;
    int actualX = (arc4random() % rangeX) + minX;
    bubble.position = ccp(actualX, 0);


    int minSpeed = 15.0;
    int maxSpeed = 20.0;
    int rangeSpeed = maxSpeed - minSpeed;
    int actualSpeed = (arc4random() % rangeSpeed) + minSpeed;


    ccBezierConfig bubblePath;
    bubblePath.controlPoint_1 = ccp(200, winSize.height/3);
    bubblePath.controlPoint_2 = ccp(-200, winSize.height/1.5);
    bubblePath.endPosition = ccp(0, winSize.height+bubble.contentSize.height/2);
    id bezierMove = [CCBezierBy actionWithDuration:actualSpeed bezier:bubblePath];

    [bubble runAction:bezierMove]; 
}}
-(void)spawBubble:(ccTime)dt {
[self addBubble];}

然后在我的 init 方法中,我添加了带有随机时间间隔的背景和 spawBubble 方法

[self schedule:@selector(spawBubble:) interval:actualTime];

我试图通过 Bubbles 打击每个气泡,当它被触摸时,使用此代码

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];

for (CCSprite *bubble in bubbles) {
CGRect bubbleRect = CGRectMake(bubble.position.x - (bubble.contentSize.width/2), 
                                   bubble.position.y - (bubble.contentSize.height/2), 
                                   bubble.contentSize.width, 
                                   bubble.contentSize.height);
    if (CGRectContainsPoint(bubbleRect, touchLocation)) {            
        NSLog(@"%i", [bubbles count]);
        [bubble setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"bubbleBlow"]];
        id disappear = [CCFadeTo actionWithDuration:0.1 opacity:0];
        [bubble runAction:disappear];            
    }
}

return TRUE;}

如果屏幕上只有一个气泡,每个气泡都会吹得很好,但如果屏幕上出现一个气泡而另一个气泡出现,那么只有最后一个气泡会检测到触摸。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

很难说没有看到更多代码 - 但这是我开始的地方:

在这一行之上:

for (CCSprite *bubble in bubbles) {

添加:

NSLog(@"%i", [bubbles count]);

编辑:

根据您添加的代码:

你的问题在于这一行:

bubbles = [[NSMutableArray alloc] init];

每次添加新气泡时都会有效地清除bubbles数组。

所以,阵列中只会有一个气泡 - 最后一个气泡。

因此你遇到了问题。

答案 1 :(得分:0)

我通过删除bubbles = [[NSMutableArray alloc] init];并将其添加到 init 方法来更改 addBubble 方法... 现在一切都很完美! )