0

我已经实现了一种在 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;
}
4

1 回答 1

0

您的代码看起来不错,只需尝试调试它:

  • 在您的图像中制作特殊颜色边框(例如粉红色)
  • 尝试使用比例 1(或模拟位置 = ccp(0,0))触摸它,如果颜色是粉红色,则尝试使用比例 0.5 触摸

还有一件事,您可能会遇到设备上纹理大小为 1x1 的问题。我曾经遇到过这样的问题,应该将纹理大小设置为 32x32:[[CCRenderTexture alloc] initWithWidth:32 height:32 pixelFormat:kCCTexture2DPixelFormat_RGBA8888];

于 2012-05-12T09:17:32.587 回答