我正在尝试将 SKLabelNode 扭曲为 SKEffectNode 的子节点(文档说您可以这样做),但它会在应用扭曲之前将标签节点倒置。这可能是一个错误,但在我提交之前,我想我会在这里问...
这是我的代码:
func warpNode(_ node: SKEffectNode)
{
if #available(iOS 10.0, *)
{
let sourcePositions: [vector_float2] =
[
vector_float2(0, 0), vector_float2(0.5, 0), vector_float2(1, 0), //bottom row of object
vector_float2(0, 0.5), vector_float2(0.5, 0.5), vector_float2(1, 0.5), //middle row of object
vector_float2(0, 1), vector_float2(0.5, 1), vector_float2(1, 1) //top row of object
]
let destinationPositions: [vector_float2] =
[
vector_float2(0, 0), vector_float2(0.5, 0), vector_float2(1, 0), //bottom row of object
vector_float2(0, 0.5), vector_float2(0.5, 0.5), vector_float2(1, 0.5), //middle row of object
vector_float2(0, 0.8), vector_float2(0.5, 0.8), vector_float2(1, 0.8) //top row of object
]
let warpGeometryGrid = SKWarpGeometryGrid(columns: 2, rows: 2, sourcePositions: sourcePositions, destinationPositions: destinationPositions)
let warpGeometryGridNoWarp = SKWarpGeometryGrid(columns: 2, rows: 2)
node.warpGeometry = warpGeometryGridNoWarp
let warpAction = SKAction.animate(withWarps: [warpGeometryGridNoWarp, warpGeometryGrid, warpGeometryGridNoWarp], times: [0.25, 0.5, 0.75], restore: true)
node.run(warpAction!)
}
else
{
print("Need iOS >= iOS 10 to warp!!!")
}
}
let effectNode = SKEffectNode()
effectNode.shouldEnableEffects = true
addChild(effectNode)
let labelNode = SKLabelNode(fontNamed: "Chalkduster")
labelNode.text = "Label"
effectNode.addChild(labelNode)
warpNode(effectNode)
这段代码应该压扁标签的顶部,然后将其返回到原来的位置(就像你推了一个球或其他东西一样),然而,它把它倒过来,然后压扁顶部(现在是底部单词),然后将其恢复到原来的大小和位置(正面朝上)。
编辑:根据 0x141E 的评论,我将 SKLabelnode 转换为 SKTexture 并添加了一个具有该纹理的新 SKSpriteNode。如果我在 effectNode 上使用 warpNode 函数,它具有将单词倒置然后应用 squish 的相同效果。但是,如果我直接在新的 SKSpriteNode 上使用 warpNode 函数,它就可以正常工作。
新代码如下:
func warpNode(_ node: SKSpriteNode)
{
if #available(iOS 10.0, *)
{
let sourcePositions: [vector_float2] =
[
vector_float2(0, 0), vector_float2(0.5, 0), vector_float2(1, 0), //bottom row of object
vector_float2(0, 0.5), vector_float2(0.5, 0.5), vector_float2(1, 0.5), //middle row of object
vector_float2(0, 1), vector_float2(0.5, 1), vector_float2(1, 1) //top row of object
]
let destinationPositions: [vector_float2] =
[
vector_float2(0, 0), vector_float2(0.5, 0), vector_float2(1, 0), //bottom row of object
vector_float2(0, 0.5), vector_float2(0.5, 0.5), vector_float2(1, 0.5), //middle row of object
vector_float2(0, 0.8), vector_float2(0.5, 0.8), vector_float2(1, 0.8) //top row of object
]
let warpGeometryGrid = SKWarpGeometryGrid(columns: 2, rows: 2, sourcePositions: sourcePositions, destinationPositions: destinationPositions)
let warpGeometryGridNoWarp = SKWarpGeometryGrid(columns: 2, rows: 2)
node.warpGeometry = warpGeometryGridNoWarp
let warpAction = SKAction.animate(withWarps: [warpGeometryGridNoWarp, warpGeometryGrid, warpGeometryGridNoWarp], times: [0.25, 0.5, 0.75], restore: true)
node.run(warpAction!)
}
else
{
print("Need iOS >= iOS 10 to warp!!!")
}
}
let labelNode = SKLabelNode(fontNamed: "Chalkduster")
labelNode.text = "Label"
let labelNodeTexture = SKView().texture(from: labelNode)!
let labelNodeSprite = SKSpriteNode(texture: labelNodeTexture)
addChild(labelNodeSprite)
warpNode(labelNodeSprite)
我只是删除了代码的整个 effectNode 部分并添加了新的精灵并对其进行了变形。