gpt4 book ai didi

ios - 简单的益智游戏 iOS : Analyzing alpha pixel issues

转载 作者:行者123 更新时间:2023-11-29 04:38:09 24 4
gpt4 key购买 nike

我创建了简单的益智游戏并使用 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 像素,我就不必选择这 block 。

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

现在我用这个:

- (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;
}
}

}

}
}
}
}

还有这项工作...

最佳答案

请注意,在第一种方法中,您出于同一目的使用了两次不同触摸。

首先你要做的:

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

用于检查您的触摸是否在片段 View 内。

但是你再次接触:

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

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

关于ios - 简单的益智游戏 iOS : Analyzing alpha pixel issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10782113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com