swift – 完成块从未在组的SKAction序列结束时调用
为什么从未调用完成?
我非常抱歉这是一个代码转储…因为我不知道为什么每个部分都有效,除了调用完成. 除了完成之外,没有调用完成的SKAction运行.就是这个: curtain.run(fadeMoveWipeAndReveal,completion: {onDone(),print("I'm done!!!!")}) 在以下课程中: import SpriteKit class WipeCurtain: SKSpriteNode { var wipeCurtainBase: SKSpriteNode? var returnable = SKNode() var moveRight = SKAction() var node = SKNode() init( color: SKColor,size: CGSize,time: TimeInterval,reveal: @escaping () -> (),onDone: @escaping () -> ()) { super.init(texture: nil,color: color,size: size) wipeCurtainBase = SKSpriteNode(color: color,size: size) let show = SKAction.run(reveal) let fadeIn = createFadeIn(duration: time) let moveRight = createMoveRight(duration: time) let wipeRight = createWipeRight(duration: time) let fadeAndMove = SKAction.group( [ fadeIn,moveRight ] ) let wipeAndReveal = SKAction.group( [ show,wipeRight ] ) let fadeMoveWipeAndReveal = SKAction.sequence( [ fadeAndMove,wipeAndReveal ] ) if let curtain = self.wipeCurtainBase { curtain.anchorPoint = CGPoint(x: 1,y: 0) curtain.position = CGPoint(x: frame.size.width,y: 0) curtain.zPosition = -1 curtain.name = "wipe" curtain.run(fadeMoveWipeAndReveal,completion: { onDone() print("I'm done!!!!") }) } } func createFadeIn(duration: TimeInterval) -> SKAction { let fadeIn = SKEase .fade( easeFunction: .curveTypeLinear,easeType: .easeTypeIn,time: duration,fromValue: 0,toValue: 1 ) return fadeIn } func createMoveRight(duration: TimeInterval) -> SKAction { let moveRight = SKEase .move( easeFunction: .curveTypeExpo,easeType: .easeTypeOut,duration: duration,origin: CGPoint( x: 0,y: 0),destin: CGPoint( x: frame.size.width,y: 0) ) return moveRight } func createWipeRight(duration: TimeInterval) -> SKAction { let wipeRight = SKEase .createFloatTween( start: 1,ender: 0,timer: duration,easer: SKEase .getEaseFunction( .curveTypeExpo,easeType: .easeTypeOut ),setterBlock: {(node,i) in node.xScale = i} ) return wipeRight } func wipeWith() -> SKNode { if let curtain = wipeCurtainBase?.copy() as! SKSpriteNode? { returnable = curtain } return returnable } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } 更新: 这是使这项工作的gameScene,Xcode SpriteKit模板的一个非常轻微的修改版本: import SpriteKit import GameplayKit class GameScene: SKScene { private var swipe: WipeCurtain? override func didMove(to view: SKView) { swipe = WipeCurtain(color: .brown,size: frame.size,time: 1,reveal: self.showOrNot,onDone: self.previewer ) } func showOrNot(){ print(" Deciding to show or not.......") } func previewer(){ print("now running the previewer") } func touchDown(atPoint pos : CGPoint) { addChild(swipe!.wipeWith()) } override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) { for t in touches { self.touchDown(atPoint: t.location(in: self)) } } } 解决方法
我想把注意力集中在以下几行:
违规代码是: curtain.run(fadeMoveWipeAndReveal,completion: { onDone() print("I'm done!!!!") }) 你可以用其他一个动作替换fadeMoveWipeAndReveal,比如show: let show = SKAction.run(reveal) 你会看到永远不会调用完成.为什么?因为它在WipeCurtain初始化期间第一次运行但操作在完成之前被删除,所以下次重新调用此操作以通过touchesBegan运行时,将永远不会调用完成. 您可以通过在curtain.run代码中放置一个断点来测试它并启动项目: 正如您所看到的那样,断点在初始化期间立即停止项目,在此阶段,操作在完成之前被删除. 关于你的解决方法,这是正确的,它是有效的,因为你删除完成并使用每次调用时正确执行的简单SKAction.sequence. 细节: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |