[英]Fit borders to “real” image, cut of white borders SpriteKit objective c
I am learning Spritekit right now and I want to detect a collision between two images. 我现在正在学习Spritekit,我想检测两个图像之间的冲突。 Just a fun picture as an example :
仅以一个有趣的图片为例:
The image is still a rectangle. 该图像仍然是一个矩形。 How can I fit it that this rectangle will fit to the original face?
我怎样才能使该矩形适合原始面? I don't want the collision early when it hits the rectangle of the Image, I want it to collide when it actually hits the black lines of the face.
我不想在碰撞到图像的矩形时尽早发生碰撞,而希望在碰撞到面部的黑线时发生碰撞。
I hope you can understand my problem. 希望你能理解我的问题。
Thanks for any help. 谢谢你的帮助。
If its an image thats got white borders all around it, u can edit it in Preview and use Alpha to get rid of all of the white around it. 如果其图像周围都有白色边框,则可以在“预览”中对其进行编辑,然后使用Alpha消除周围所有白色。
I think in that case, the sprite's collision method will collide with the black lines. 我认为在这种情况下,精灵的碰撞方法将与黑线发生碰撞。
There are two issues here: 这里有两个问题:
You probably want to set up your SKPhysicsBody
to be a path around your SKSpriteNode
. 你可能想设置你的
SKPhysicsBody
是在你的路径SKSpriteNode
。 For example, something like the following is close: 例如,类似以下的内容很接近:
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-40, -22, 80, 39)]; node.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path.CGPath];
That's probably not quite right, so feel free to tweak those values, but hopefully it illustrates the idea: Come up with a path that defines your boundary, and then set the physicsBody
accordingly. 那可能不太正确,因此可以随意调整这些值,但希望它能说明这一想法:提出一条定义边界的路径,然后相应地设置
physicsBody
。
If you want to see the physics outlines, set showsPhysics
on the SKView
. 如果要查看物理轮廓,请在
showsPhysics
上设置SKView
。
You probably want to mask your image so that the corners of the image are transparent (ie have an alpha of zero): 您可能需要遮罩图像,以使图像的角是透明的(即alpha值为零):
So, when that's on a colored background it looks like: 因此,当在彩色背景上时,它看起来像:
By masking the corners, you don't have to worry about the white corner covering up something else on the scene. 通过遮盖角落,您不必担心白色角落会掩盖场景中的其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.