0

我创建了简单的益智游戏,并为此使用 next:

我有一个名为 Piece 的课程。Piece 类继承自UIImageView并包含下一个字段:

@property(nonatomic, strong) NSString *pieceName;
@property(nonatomic) CGFloat pos_x;
@property(nonatomic) CGFloat pos_y;
@property(nonatomic) BOOL snapped;

在我的游戏中,每个棋子都有颜色(红色、绿色、蓝色等)。每一块都有不规则的形状。这是带有 alpha 颜色的图像。当我点击 alpha 颜色时,我不必选择这件作品。

为此,我使用下一个方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    NSArray *views = [self.view subviews];

    for (UIView *v in views) {
        if([v isKindOfClass:[Piece class]]){
            if (CGRectContainsPoint(v.frame, touchLocation) && ((Piece *)v).snapped == FALSE) { 

                UITouch* touchInPiece = [touches anyObject]; 
                CGPoint point = [touchInPiece locationInView:(Piece *)v];
                BOOL solidColor = [self verifyAlphaPixelImage:(Piece *)v atX:point.x * resolution atY:point.y * resolution];

                if (solidColor) {                

                    NSArray *tempViews = [self.view subviews];
                    [tempViews reverseObjectEnumerator];

                    for (UIView *tv in tempViews) {
                        if (tv == v) {
                            [tv setExclusiveTouch:YES];
                            dragging = YES;
                            oldX = touchLocation.x;
                            oldY = touchLocation.y;
                            piece = (Piece *)v;
                            [self.view bringSubviewToFront:piece];
                            break;
                        }
                    }

               }

            }
        }
    }
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (dragging) {        
        CGRect frame = piece.frame;
        frame.origin.x = piece.frame.origin.x + touchLocation.x - oldX;
        frame.origin.y =  piece.frame.origin.y + touchLocation.y - oldY;
        if ([self verifyOutOfScopeCoordX:touchLocation.x CoordY:touchLocation.y]) {
            piece.frame = frame;
        }               
        [self snapPiece];
    }

    oldX = touchLocation.x;
    oldY = touchLocation.y;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{    
    dragging = NO;
}

- (BOOL)verifyAlphaPixelImage:(Piece *)image atX:(int)x atY:(int)y{

    CGImageRef imageRef = [image.image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);

    int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;

    CGFloat red   = (rawData[byteIndex]     * 1.0) / 255.0;
    CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
    CGFloat blue  = (rawData[byteIndex + 2] * 1.0) / 255.0;
    CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
    /*
    NSLog(@"red:   %f", red);
    NSLog(@"green: %f", green);
    NSLog(@"blue:  %f", blue);
    NSLog(@"alpha:   %f", alpha);
    */    
    free(rawData);    
    if(alpha==0 && red == 0 && green == 0 && blue == 0) return NO;
    else return YES;

}

我移动我的棋子没有任何问题。但有时我会verifyAlphaPixelImage在这一行中使用一种方法得到奇怪的错误。CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;EXC_BAD_ACCESS

verifyAlphaPixelImage - 我需要这种方法来分析 alpha 像素。如果我点击 alpha 像素,我不必选择这件作品。

现在主要问题是EXC_BAD_ACCESS我使用 rawData 的在线。

现在我用这个:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    NSArray *views = [self.view subviews];

    for (UIView *v in views) {
        if([v isKindOfClass:[Piece class]]){

            CGFloat pos_x = touchLocation.x - v.frame.origin.x;
            CGFloat pos_y = touchLocation.y - v.frame.origin.y;         

            if (CGRectContainsPoint(v.frame, touchLocation) && ((Piece *)v).snapped == FALSE) { 


                BOOL solidColor = [self verifyAlphaPixelImage:(Piece *)v atX:pos_x * resolution atY:pos_y * resolution];

                if (solidColor) {                

                    NSArray *tempViews = [self.view subviews];
                    [tempViews reverseObjectEnumerator];

                    for (UIView *tv in tempViews) {
                        if (tv == v) {
                            [tv setExclusiveTouch:YES];
                            dragging = YES;
                            oldX = touchLocation.x;
                            oldY = touchLocation.y;
                            piece = (Piece *)v;
                            [self.view bringSubviewToFront:piece];
                            break;
                        }
                    }

                }

            }
        }
    }
}

而这项工作...

4

1 回答 1

1

请注意,在您的第一种方法中,您使用两种不同的触摸来实现相同的目的。

首先你做:

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];

用于检查您的触摸是否在一个片段视图内。

但是然后你再接触一下:

UITouch* touchInPiece = [touches anyObject]; 
CGPoint point = [touchInPiece locationInView:(Piece *)v];

如果您有多个触摸事件,则第二次触摸可能不是第一次触摸,甚至可能不在检测到第一次触摸的视图内。第二次触摸可能超出第一次触摸检测到的片段视图的范围

于 2012-05-28T13:29:40.223 回答