我想在遊戲地圖上精確選擇我的對象。對象是一個精靈,它周圍有一些透明的像素,我想測試這些透明像素的觸摸位置。 任何線索?如何從SKSpriteNode或SKTexture獲取像素顏色?
3
A
回答
3
在Sprite Kit中,您無權訪問紋理數據。
相反,您必須從圖像創建一個位掩碼,例如通過將圖像加載爲UIImage或CGImage並掃描所有非透明像素。
2
您可以繼承SKSpriteNode的子類,在檢測觸摸時添加對圖像的引用並檢查該圖像的alpha。
1)添加一個實例變量來保存你的形象
@implementation DraggableSprite {
UIImage *_holdingImage;
}
2)然後在init方法可以保存參考該圖像
-(id)initWithImageNamed:(NSString *)name {
if (self=[super initWithImageNamed:name]) {
self.userInteractionEnabled = YES;//enable touches handling
self.anchorPoint = P(0,0);
_holdingImage = [UIImage imageNamed:name];
}
return self;
}
3)檢測的觸摸
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGFloat a = [UIImage getAlphaFromImage:_holdingImage atX:positionInScene.x andY:self.size.height-positionInScene.y];
NSLog(@"alpha is %f",a);
}
4)阿爾法檢查功能
+(CGFloat)getAlphaFromImage:(UIImage*)image atX:(NSInteger)xx andY:(NSInteger)yy {
// Cancel if point is outside image coordinates
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), CGPointMake(xx,yy))) {
return 0;
}
// Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
// Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
NSInteger pointX = xx;
NSInteger pointY = yy;
CGImageRef cgImage = image.CGImage;
NSUInteger width = image.size.width;
NSUInteger height = image.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * 1;
NSUInteger bitsPerComponent = 8;
unsigned char pixelData[4] = { 0, 0, 0, 0 };
CGContextRef context = CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);
// Draw the pixel we are interested in onto the bitmap context
CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
CGContextRelease(context);
CGFloat alpha = (CGFloat)pixelData[3]/255.0f;
return alpha;
}
+0
我在'let scene = ...'之後立即用以下兩行修改了你的代碼:view.allowsTransparency = true; scene.backgroundColor = UIColor(紅色:0,綠色:0,藍色:0,alpha:0)。否則,你會得到標準的背景alpha +顏色。 –
2
這裏是如何直接從SKTexture得到一個像素的顏色:
class SpriteKitUtility
{
class func pixelFromTexture(texture: SKTexture, position: CGPoint) -> SKColor {
let view = SKView(frame: CGRectMake(0, 0, 1, 1))
let scene = SKScene(size: CGSize(width: 1, height: 1))
let sprite = SKSpriteNode(texture: texture)
sprite.anchorPoint = CGPointZero
sprite.position = CGPoint(x: -floor(position.x), y: -floor(position.y))
scene.anchorPoint = CGPointZero
scene.addChild(sprite)
view.presentScene(scene)
var pixel: [Byte] = [0, 0, 0, 0]
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, CGColorSpaceCreateDeviceRGB(), bitmapInfo);
UIGraphicsPushContext(context);
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
UIGraphicsPopContext()
return SKColor(red: CGFloat(pixel[0])/255.0, green: CGFloat(pixel[1])/255.0, blue: CGFloat(pixel[2])/255.0, alpha: CGFloat(pixel[3])/255.0)
}
}
+0
這看起來如何迅速3,我無法切換它。 – bluelemonade
相關問題
- 1. SpriteKit:如何從SKSpriteNode中的點獲取像素顏色?
- 2. 如何從像素獲取顏色? OpenGL
- 3. 從圖像中獲取像素顏色
- 4. 獲取AVCaptureSession或AVCaptureVideoPreviewLayer的像素顏色
- 5. WriteableImage - 如何獲取像素的顏色?
- 6. FreeImage:獲取像素顏色
- 7. GDAL獲取像素顏色
- 8. Java - 獲取像素顏色
- 9. 從Kinect Depth Stream獲取像素顏色
- 10. 從CCRenderTexture獲取像素的顏色
- 11. 從NSImage獲取像素和顏色
- 12. 從圖片中獲取像素顏色?
- 13. 如何從圖像中獲取像素的顏色?
- 14. 如何從SKSpriteNode獲取圖像名稱?
- 15. 如何獲取圖像中像素的顏色(加載灰色)?
- 16. 如何更改沒有SKTexture的didBeginContact中的SKSpriteNode大小和顏色?
- 17. libGDX從精靈或紋理獲取像素顏色
- 18. 如何獲取圖像輪廓(或從圖像中獲取特定顏色?)-ANDroid
- 19. 如何在SKSpriteNode中「中心」SKTexture
- 20. 如何從Openlayers 3圖層獲取像素的顏色值?
- 21. Android,如何從Canvas獲取像素顏色?
- 22. 如何從圖形獲取像素顏色g
- 23. 如何從React Native的屏幕上的像素獲取顏色?
- 24. 如何從WriteableBitmap獲取像素的顏色?
- 25. 如何從圖片框中獲取特定像素的顏色?
- 26. 使用pixeldata從SKTexture創建SKSpriteNode
- 27. 硒 - 如何獲得像素顏色?
- 28. 嘗試獲取像素顏色
- 29. 獲取UIImage的像素顏色
- 30. Java獲取像素的顏色LIVE
哪個是較好的一類用於此目的?這兩個類中的哪一個在像素級處理圖像時具有更好的性能(和性能)? – prototypical