swift - SpriteKit 。停止使用 CameraNode 旋转纹理

标签 swift sprite-kit

我正在做一个小游戏,让汽车绕地球转。相机始终以与行星成 90 度角的方式对准机器。我遇到了行星纹理随相机旋转的问题。如果相机不旋转,则纹理表现正常。我该如何解决这个问题?

从 beizerPath 创建行星:

planetTexture = SKTexture(imageNamed: "asanoha")

planet = SKShapeNode(path: beizerPath.cgPath)
planet.position = CGPoint(x: 0, y: 0)
planet.zPosition = 10
planet.physicsBody = SKPhysicsBody(polygonFrom: beizerPath.cgPath)
planet.physicsBody?.isDynamic = false
planet.physicsBody?.categoryBitMask = planetGroup
planet.fillColor = SKColor.white
planet.fillTexture = planetTexture
planet.strokeColor = getRandomColor()
planet.lineWidth = 7
planet.glowWidth = 0.5

sceneNode.addChild(planet)

相机位置:

var cam = SKCameraNode()

override func didMove(to view: SKView) {
    self.camera = cam
}

let angle = atan2(car.position.y, car.position.x)
let theta : CGFloat = 0.0

let cx = (Float(a) * cosf(Float(angle - theta)))
let cy = (Float(a) * sinf(Float(angle - theta)))

cam.position = CGPoint(x: CGFloat(cx), y: CGFloat(cy))
cam.zRotation = angle - CGFloat(M_PI_2)

My problem on video

最佳答案

这样做的原因是形状填充是在屏幕空间中完成的。纹理未映射到具有纹理坐标的 SKShapeNode,它被视为填充颜色。

解决方案是使用SKSpriteNodeSKCropNode。如果您使用 sprite 节点,您的图像将已经在边缘周围具有透明度,以获得行星的圆形形状。如果您使用裁剪节点,您可以添加一个方形 Sprite 节点作为子节点,然后将其蒙版节点属性设置为您的形状节点 - 这将决定渲染的蒙版部分。

裁剪节点的方法是这样的:

planetTexture = SKTexture(imageNamed: "asanoha")
planetSprite = SKSpriteNode(texture: planetTexture)
planetMask = SKShapeNode(path: beizerPath.cgPath)
planetMask.fillColor = UIColor.white
planet = SKCropNode()
planet.maskNode = planetMask
planet.addChild(planetSprite)
sceneNode.addChild(planet)

更新:我刚看了您的视频,发现我的回答对外观做了一些不正确的假设。然而,裁剪节点方法仍然有效,只是您需要稍微不同地设置 sprite 节点,以便纹理重复。具有透明度的纯 Sprite 不适用于如此巨大且不规则(并且可能是生成的)行星。

关于swift - SpriteKit 。停止使用 CameraNode 旋转纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39576317/

相关文章:

ios - 如何在调整 UIImage 大小时提高清晰度?

cocoa - “NSTextView”没有名为 'setString' 的成员

arrays - 使用字符串数组作为值在swift中创建字典

ios - 无法让 SKActionmove 可靠地工作

ios - 如何在 SpriteKit 中制作响应式 Controller ?

ios - SpriteKit : Why does it wait one round for the score to update?( swift )

ios - 如何在点击后更改 UIButton 图像?

ios - Swift、单例、代表函数的变量

ios - 大 SKSpriteNode 导致帧率下降

memory-leaks - 在 GameplayKit 中使用 GKStateMachine 的泄漏