SKLabelNode上的SKWarpGeometry作为SKEffectNode的子项翻转标签颠倒

时间:2016-11-22 23:28:20

标签: swift sprite-kit sklabelnode skeffectnode

我试图将SKLabelNode作为SKEffectNode的子项进行扭曲(文档说你可以这样做),但它会在应用warp之前翻转标签节点。这可能是一个错误,但在我提交之前,我以为我会问这里......

这是我的代码:

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函数,它具有相同的效果,即翻转单词然后应用压扁。但是,如果我直接在新的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部分并添加了新的sprite并对其进行了扭曲。

2 个答案:

答案 0 :(得分:1)

根据0x141E的评论编辑我的问题作为答案。

答案 1 :(得分:0)

如果您始终使标签节点变形(连续对其进行动画处理或最初应用变形),则可以使用values来抢先翻转标签节点。然后,当您弯曲时,它会朝上。有点愚蠢,但这对我有用。