簡體   English   中英

如何在cocos2d中選擇所有帶有觸摸的目標

[英]How To select all the targets with touches rect in cocos2d

我想使用觸摸矩形選擇我的目標

通過以下代碼創建我的未選擇點:-

    targets1 = [[NSMutableArray alloc] init];

         for(int i=0;i<3;i++)
            {
                for (int y=0; y<3; y++) {
                    CCTexture2D *texture = 
                    [[CCTextureCache sharedTextureCache] addImage:@"UnselectedDot.png"];
                        block = [CCSprite spriteWithTexture:texture rect:CGRectMake(0,0,82,82)];
                    CGFloat xoffset = ((block.contentSize.width)*10) + (((block.contentSize.height)-175)*y);
                    block.position = ccp( (i*82)+80,xoffset);
                   [bg1 addChild:block];

                    [targets1 addObject:block];
}
}

以下是我的示例輸出。

現在我需要通過觸摸方法選擇所有的點。 我寫了這樣的代碼:

- (void)update:(ccTime)dt {
 //   NSLog(@"%@",targets1);
   for (CCSprite *sprite in targets1) {
       CGRect dotrect = CGRectMake(sprite.position.x, 
                             sprite.position.y-95, 
                             sprite.contentSize.width, 
                             sprite.contentSize.height);
       CGFloat x = location.x;
       CGFloat y = location.y;
       CGFloat width =  (location1.x - location.x);
       CGFloat height = -(location1.y - location.y);
       CGRect touchrect = CGRectMake (x, y, width,height); 
       NSLog(@"dotrect = %f,%f,%f,%f",dotrect.origin.x,dotrect.origin.y,dotrect.size.width,dotrect.size.height );                        
       NSLog(@"touch rect = %f,%f,%f,%f,%f,%f",touchrect.origin.x,touchrect.origin.y,touchrect.size.width,touchrect.size.height,location1.x,location1.y);
       if( CGRectContainsRect(dotrect, touchrect))
       {     //collision detection
            NSLog(@"am touched dot ");
       }
     }
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {    
    location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location];
    NSLog(@"am touched began");
    return YES;

}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    location1 = [touch locationInView:[touch view]]; 
    location1 = [[CCDirector sharedDirector] convertToGL:location1];
   location1 = [self convertToNodeSpace:location1]; 
}

按照上面的編碼,我的概念是:-使用觸摸開始和觸摸結束在這里制作矩形..在這些touchrect中,我未選擇的點rect表示檢測到collsison。 但它不會全部碰撞。

我沒有犯錯誤。

編輯:1

現在我明白了為什么線圈損壞不起作用...實際上我的內在接觸直腸有多個直腸..所以...在這里,我使用rectcontainsrect ..解決了這個問題..任何其他方法來矯正具有多個直腸以進行碰撞檢測。

嘗試使用:

CGPoint location=[touch locationInView:[touch view]];
    location = [self convertTouchToNodeSpace:touch];
CGRectContainsPoint([dot boundingBox], location);

在CCTouch ..方法內部(根據您的意願進行操作)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM