2012-05-09 6 views
0

私は、cocos2dでピクセルの完全な衝突を見つける方法を実装しました。しかし、オブジェクトのピクセルを完全な衝突が働かないようにスケールしなければならない場合。cocos2dのスケーリングされたスプライトでピクセルの完全な衝突を見つける方法は?

 //sprite declaration 
    fruit1=[CCSprite spriteWithSpriteFrameName:@"Burger-Cut1.png"]; 
    fruit1.position=ccp(200,100); 
    [self addChild:fruit1]; 

    fruit.scale=0.5; 


    //find the collision 

    -(bool) isTransparentWithSprite: (CCSprite *)sprite pointInNodeSpace: (CGPoint) point { 
NSLog(@"in function"); 
bool isTransparent = YES; 

unsigned int w = 1; 
unsigned int h = 1; 
unsigned int wh = w * h; 
//ccColor4B *buffer = ccColor4B[4]; 
Byte buffer[4]; 
// Draw into the RenderTexture 
[rt beginWithClear:0 g:0 b:0 a:0]; 

CGPoint oldPos = sprite.position; 
CGPoint oldAnchor = sprite.anchorPoint; 
sprite.anchorPoint = ccp(0, 0); 
sprite.position = ccp(-point.x, -point.y); 

[sprite visit]; 
sprite.anchorPoint = oldAnchor; 
sprite.position = oldPos; 

// read the pixels 
float x = 0;//local.x; 
float y = 0;//local.y; 
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 
//NSLog(@"%d: (%f, %f)", sprite.tag, x, y); 
[rt end]; 
// Read buffer 
ccColor4B color; 
for(unsigned int i=0; i<wh; i++) { 
    //color = buffer[i]; 
    // if untouched, point will be (0, 0, 0, 0). if the sprite was drawn into this pixel (ie not transparent), one of those values will be altered 
    if (buffer[0] || buffer[1] || buffer[2] || buffer[3]) { 
     NSLog(@" "); 
     NSLog(@"%d %d %d %d=== > ",buffer[0],buffer[1],buffer[2],buffer[3]); 
     NSLog(@" "); 
     isTransparent = NO; 
    } 
} 
//delete buffer; 
return isTransparent; 
} 
+0

どのようにisTransparentWithSpriteメソッドにポイントを送信しますか?このようなもの:[self convertTouchToNodeSpace:touch]? –

+0

これは私のコードです:UITouch * myTouch = [touches anyObject]; CGPointロケーション= [myTouch locationInView:[myTouch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; CGPoint local = [スプライトconvertToNodeSpace:タッチ位置]; [自己isTransparentWithSprite:スプライトpointInNodeSpace:ローカル] – banu

答えて

0

あなたのコードは大丈夫に見えますが、それをデバッグしよう:

  • (例えばピンク)自分の画像に特殊な色の境界線を作るスケール1でそれに触れる(または場所をシミュレートしてみてください= ccp(0,0))、色がピンクの場合は、縮尺で触れてください。012

さらにもう1つ、デバイスのテクスチャサイズ1x1に問題があります。私はそのような問題を一度だけ持ち、テクスチャサイズを32x32に設定する必要があります:[[CCRenderTexture alloc] initWithWidth:32 height:32 pixelFormat:kCCTexture2DPixelFormat_RGBA8888];

関連する問題