obj-c to swift? weird results

all,


i thought this would be trivial so i threw this simple code against a couple of online converters, both crashed and burned on trying to convert this to swift 3.1


i pilfered this code somewhere, i forgot but it seems likely to work, i just want a simple SKTexture fadein/fadeout transition on a spritenode, is this too much to ask for? maybe.


-(void) fadeTexture:(SKTexture *)newTexture ontoSpriteNode:(SKSpriteNode *)referenceSpriteNode withDuration:(CFTimeInterval)duration {

    SKSpriteNode * fadeInSprite = [self fadeInSpriteWithTexture:newTexture referenceSpriteNode:referenceSpriteNode];

    [[referenceSpriteNode parent] addChild:fadeInSprite];
    [fadeInSprite runAction:[SKAction sequence:@[
        [SKAction fadeAlphaTo:1 duration:duration],
        [SKAction runBlock:^{
            [fadeInSprite removeFromParent];
            [referenceSpriteNode setTexture:newTexture];
        }]
    ]]];
}


one site returned

/
Unexpected :  :

Expected   :
',', ';'
*/


another site returned something reasonable but got lost in the "optional, opaque, escaping block wilderness"


//  Converted with Swiftify v1.0.6402 - https://objectivec2swift.com/
func fade(_ newTexture: SKTexture, ontoSpriteNode referenceSpriteNode: SKSpriteNode, withDuration duration: CFTimeInterval) {
    let fadeInSprite: SKSpriteNode? = self.fadeInSprite(with: newTexture, referenceSpriteNode: referenceSpriteNode)
    referenceSpriteNode.parent?.addChild(fadeInSprite!)
    fadeInSprite?.runAction(SKAction.sequence([SKAction.fadeAlpha(to: 1, duration: duration as? TimeInterval ?? TimeInterval()), SKAction.run({0:@escaping ()]))
}

What is your actual question?


If you're saying you're stumped by trying to put together nested SKActions (which is painful to do, I agree), start by constructing simple actions from the inside out. Assign each one to a local variable. Then combine them step by step, also in local variables. Finally, issue the outer "runAction:" on the top-level local variable.


Once you have all the local variable "pieces" keeping your syntax straight, then you can look to eliminating some or all by substituting RHS expressions for LHS variable. Or just leave your "working out" as document for future readers of the code.

The question is how to create a simple SKTexture transition with fadein/fadeout on a spritenode with large array of hi-res textures? and how to force the GPU to flush its buffer/cache?


I was hoping someone would point out the mistake in the obj-C to swift conversion as I think a group is needed in addition to a sequence or if someone already has a filter graph to visually chain together options like blender does.


These methods do not work as they bomb due to insufficient memory, as textures are large 4k x 2k and numerous 30+


definition bomb = after 2 images are displayed on screen, sknode goes clear and motion of said node freezes requiring restart of app


iPad has 128Gb of ram with A8 gpu


1 SKTexture.preload(my4kArray)

2 SKTextureAtlas.preload(my4kArray), specifying maximum texture in the build options did not improve

3 SKAction.animate

Here is the output from the current version of Swiftify:

http://swiftify.me/fst0gz


While there are still things to improve, the code should compile as is:

//  Converted to Swift 4 with Swiftify v1.0.6536 - https://objectivec2swift.com/
func fade(_ newTexture: SKTexture, ontoSpriteNode referenceSpriteNode: SKSpriteNode, withDuration duration: CFTimeInterval) {
    let fadeInSprite: SKSpriteNode? = self.fadeInSprite(with: newTexture, referenceSpriteNode: referenceSpriteNode)
    referenceSpriteNode.parent?.addChild(fadeInSprite ?? SKNode())
    fadeInSprite?.run(SKAction.sequence([SKAction.fadeAlpha(to: 1, duration: duration as? TimeInterval ?? 0.0), SKAction.run({() -> Void in
        fadeInSprite?.removeFromParent()
        referenceSpriteNode.texture = newTexture
    })]))
}
obj-c to swift? weird results
 
 
Q