如何在我的精灵套件游戏中获得3点的颜色数据?

时间:2015-06-22 01:07:37

标签: swift sprite-kit cgimage

我正试图在2d快速游戏中模拟一个跟随线的机器人。

现在,这是截至目前为止的截图:screenshot

黄色的东西是机器人,3个透明圆圈应该是它的传感器。

现在我希望能够获得这3个透明圆的中心像素的颜色数据。因为我想知道这三个“传感器”是否“感应”了红线或白色背景。但对于我的生活,我无法弄清楚如何做到这一点。

我在stackoverflow上找到了这个代码(修改了一下以适合我的项目):

extension SKTexture {
    func getPixelColor(pos: CGPoint) -> SKColor {

        let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
        var data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        let pixelInfo: Int = ((Int(self.size().width) * Int(pos.y)) + Int(pos.x)) * 4

        let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
        let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
        let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
        let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

        return SKColor(red: r, green: g, blue: b, alpha: a)
    }
}

但是当我打电话给那个函数时,我得到了:

2015-06-22 02:47:04.012 SKTryout[16325:1119579] -[SKTexture CGImage]: unrecognized selector sent to instance 0x6080001c0690
2015-06-22 02:47:04.016 SKTryout[16325:1119579] -[SKTexture CGImage]: unrecognized selector sent to instance 0x6080001c0690
2015-06-22 02:47:04.054 SKTryout[16325:1119579] (
    0   CoreFoundation                      0x00007fff88cec03c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff81f0e76e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff88cef0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00007fff88c34e24 ___forwarding___ + 1028
    4   CoreFoundation                      0x00007fff88c34998 _CF_forwarding_prep_0 + 120
    5   SKTryout                            0x0000000100003669 _TFE8SKTryoutCSo9SKTexture13getPixelColorfS0_FVSC7CGPointCSo7NSColor + 57
    6   SKTryout                            0x0000000100003a86 _TToFE8SKTryoutCSo9SKTexture13getPixelColorfS0_FVSC7CGPointCSo7NSColor + 54
    7   SKTryout                            0x000000010000139c _TFC8SKTryout9GameScene7keyDownfS0_FCSo7NSEventT_ + 444
    8   SKTryout                            0x00000001000013fa _TToFC8SKTryout9GameScene7keyDownfS0_FCSo7NSEventT_ + 58
    9   SpriteKit                           0x0000000100041fc4 -[SKView keyDown:] + 67
    10  AppKit                              0x00007fff8ea0e11b -[NSWindow _reallySendEvent:isDelayedEvent:] + 5452
    11  AppKit                              0x00007fff8e39fd76 -[NSWindow sendEvent:] + 470
    12  AppKit                              0x00007fff8e39c9b1 -[NSApplication sendEvent:] + 4199
    13  AppKit                              0x00007fff8e2c5c68 -[NSApplication run] + 711
    14  AppKit                              0x00007fff8e242354 NSApplicationMain + 1832
    15  SKTryout                            0x0000000100002ef7 main + 87
    16  libdyld.dylib                       0x00007fff84bf35c9 start + 1

任何人都可以告诉我我应该做什么吗?

是的,如果重要的话,我在XCode 7(Beta)和Swift 2中这样做。

2 个答案:

答案 0 :(得分:3)

CGImage方法仅在iOS 9 / OS X 10.11中引入SKTexture。您没有运行OS X 10.11 - 您仍然回到OS X 10.10 - 因此您的计算机上不存在此方法。

答案 1 :(得分:0)

修正了它:

func getPixelColor(name: String, pos: CGPoint) -> SKColor? {
    guard let img = NSImage(named: name) else {return nil}

    var pointer = CGRect(origin: CGPoint(x: 0, y: 0), size: img.size)

    guard let pixel = img.CGImageForProposedRect(&pointer, context: nil, hints: nil) else {return nil}

    let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(pixel.takeRetainedValue()))
    let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

    let pixelInfo: Int = ((Int(img.size.width) * Int(pos.y)) + Int(pos.x)) * 4

    let r = CGFloat(data[pixelInfo+0]) / CGFloat(255.0)
    let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
    let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
    let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

    return SKColor(red: r, green: g, blue: b, alpha: a)
}