SpriteKit:如何从SKSpriteNode中的某个点获取像素颜色? [英] SpriteKit: How can I get the pixel color from a point in SKSpriteNode?

查看:129
本文介绍了SpriteKit:如何从SKSpriteNode中的某个点获取像素颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个迷宫游戏,我正在使用SKSpriteNode作为实际的2d迷宫。

I'm making a maze game and i'm using a SKSpriteNode as the actual 2d maze.

我想检测SKSpriteNode上的点是否是用户触摸是黑色或白色。我创建了一个与SKSpriteNode相同的UIImage,我在UIImage上使用一个方法来获取像素信息。但是,与SKSpriteNode相比,UIImage似乎是偏移的。当我在屏幕上移动手指时它会返回值,但这是不正确的。我猜UIImage与SKSpriteNode的大小和位置不同。

I want to detect if the point on the SKSpriteNode a user is touching is black or white. I've made a UIImage which is the same image as the SKSpriteNode, and I'm using a method on the UIImage to get the pixel info. However, the UIImage seems offset compared to the SKSpriteNode. It's returning values as I move my finger around the screen, but it's incorrect. I'm guessing the UIImage is not the same size and position as the SKSpriteNode.

如何解决此问题?

我使用以下内容获取像素数据

I use the following to get the pixel data

extension UIImage {
func getPixelColor(pos: CGPoint) -> UIColor {

    let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
    let 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 UIColor(red: r, green: g, blue: b, alpha: a)
}

}

我已经像这样制作了我的SKSpriteNode

I've made my SKSpriteNode like this

var theColor : UIColor = UIColor()
var bgImage = SKSpriteNode(imageNamed: mazeImageName)
var mazeMap : UIImage = UIImage(named: mazeImageName)!

override func didMoveToView(view: SKView) {

    bgImage.position = CGPointMake(self.size.width/2, self.size.height/2)
    bgImage.size = CGSize(width: self.size.width, height: self.size.height)

    self.addChild(bgImage)

}

这里是我实际获得像素颜色的地方

And here is where I actually get the pixel color

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first {

        theColor = (mazeMap.getPixelColor(touch.locationInNode(self)))

        var red : CGFloat = CGFloat()
        var green : CGFloat = CGFloat()
        var blue : CGFloat = CGFloat()
        var alpha : CGFloat = CGFloat()

        theColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

        print("Red: \(red), Green: \(green), Blue: \(blue), Alpha: \(alpha)")

    }

}


推荐答案

是您的图片大小与 SKScene 的大小相同?

Is the size of your image the same size as your SKScene?

touchesBegan中功能,尝试在 bgImage 内获取触摸位置,而不是 SKScene

In touchesBegan function, Try getting the location of your touch inside bgImage instead of the SKScene.

theColor = (mazeMap.getPixelColor(touch.locationInNode(bgImage)))

这篇关于SpriteKit:如何从SKSpriteNode中的某个点获取像素颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆