Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在cocos2d中找到缩放精灵中的像素完美碰撞?_Iphone_Ios_Cocos2d Iphone - Fatal编程技术网

Iphone 如何在cocos2d中找到缩放精灵中的像素完美碰撞?

Iphone 如何在cocos2d中找到缩放精灵中的像素完美碰撞?,iphone,ios,cocos2d-iphone,Iphone,Ios,Cocos2d Iphone,我已经实现了一种在cocos2d中找到像素完美碰撞的方法。但是,如果必须缩放对象像素,则完美碰撞不起作用 //sprite declaration fruit1=[CCSprite spriteWithSpriteFrameName:@"Burger-Cut1.png"]; fruit1.position=ccp(200,100); [self addChild:fruit1]; fruit.scale=0.5; //find the c

我已经实现了一种在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;
}
//sprite声明
水果1=[CCSprite SpriteWithPriteFrameName:@“Burger-Cut1.png”];
结果1.位置=ccp(200100);
[自我添加儿童:结果1];
水果。比例=0.5;
//找到碰撞点
-(bool)isTransparentWithSprite:(CCSprite*)sprite点InDespace:(CGPoint)点{
NSLog(@“功能中”);
bool isTransparent=是;
无符号整数w=1;
无符号整数h=1;
无符号整数wh=w*h;
//ccColor4B*缓冲区=ccColor4B[4];
字节缓冲区[4];
//绘制到渲染器中
[rt开始于Clear:0g:0b:0a:0];
CGPoint oldPos=sprite.position;
CGPoint oldAnchor=sprite.anchorPoint;
sprite.anchorPoint=ccp(0,0);
sprite.position=ccp(-x点,-y点);
[精灵之旅];
sprite.anchorPoint=旧锚;
sprite.position=oldPos;
//读取像素
float x=0;//local.x;
float y=0;//local.y;
glReadPixels(x、y、w、h、GL_RGBA、GL_无符号字节、缓冲区);
//NSLog(@“%d:(%f,%f)”,sprite.tag,x,y);
[右端];
//读取缓冲区
CC4b颜色;

对于(unsigned int i=0;i您的代码看起来正常,请尝试调试它:

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

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

如何将点发送到方法isTransparentWithSprite?类似这样的内容:[self-ConvertTouchNodeSpace:touch]?这是我的代码:UITouch*myTouch=[Touchs anyObject];CGPoint位置=[myTouch locationInView:[myTouch视图]];位置=[CCDirector sharedDirector]convertToGL:location];CGPoint本地=[sprite convertToNodeSpace:touchLocation];[self-isTransparentWithSprite:sprite点Innodespace:local]