I'm trying to simulate a line-following robot in a 2d swift game.
Now here's a screenshot of what it looks like so far:
The yellow thingy is the robot and the 3 transparent circles are supposed to be its sensors.
Now I want to be able to get the color data of the center pixels of those 3 transparent circles. Because I want to know if those three "sensors" are "sensing" either the red line or the white background. But for the life of me I can't figure out how to do that.
I found this code here on stackoverflow (modified it a bit to fit my project):
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)
}
}
But when I call that function I get:
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
Can anyone tell me what I should be doing instead?
Btw, in case it matters, I'm doing this in XCode 7 (Beta) and Swift 2.
Fixed it with this: