2012-01-26 7 views
0

でRECT私はタッチを使用して、私のターゲットを選択したいこれらのようにコーディングすることによって、私の選択されていないドットを作成していはcocos2d

をRECT: -

以下
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 iは、その後..手段が検出collsison来ましたそこに私のものをすることができます。そのすべてが衝突することはありません。

私は間違っていました。

編集:1

は、今私がなぜ実際にinsidemyタッチは、複数の矩形を持つRECT ...動作していないcoillisionました..これだけ...ここ.. rectcontainsrectを使用していても問題はtahts ..他の方法.. colllsion検出のためのいくつかの矩形を持つRECTする

+0

あなたの結果は何ですか?ブレークポイントを追加するとタッチの開始/終了方法がヒットしますか? – nycynik

+0

@nycynik実際に私のiPhoneの画面で正方形の形式をドラッグしようとすると..何も起こっていない...同じitryもし単一の選択されていない画像の何かの衝突検出..何か私はドット毎に変化する必要があります..私のタッチでrect ... –

答えて

0

使用してみてください:CCTouch内部

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

を..方法を(あなたが何をしたいかに応じて、望むように)。