ios - 隐藏时淡入

标签 ios swift sprite-kit

当我第一次触摸屏幕时,我试图让一个对象淡入到我的游戏中,但我认为因为它之前是隐藏的(当游戏启动时),所以它不会淡入,而是只显示而没有任何动画。

你有什么建议吗?


这是一个示例代码:

import SpriteKit

class GameScene: SKScene {

    var myLabel = SKLabelNode()
    var gameStarted = Bool()

    func setupMyLabel(){
        myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "Hello, World!"
        myLabel.fontSize = 35
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
    }

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        setupMyLabel()
        self.addChild(myLabel)
        myLabel.hidden = true

        gameStarted = false
    }

    func startGame(){
        myLabel.hidden = false
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */

        if gameStarted == false{

            gameStarted = true

            startGame()
            self.myLabel.runAction(SKAction.fadeInWithDuration(2.0))
        }
        else{
            //do nothing
        }
    }
}

最佳答案

根据 Apple 的 documentationfadeInWithDuration 上声明:

When the action executes, the node’s alpha property animates from its current value to 1.0.

所以你认为这是对的,因为你的节点在启动时是隐藏的。 =)

一种可能的解决方案是不将节点的隐藏属性设置为 true,而是将其 alpha 值设置为 0。或者您甚至可以创建自己的方法来执行,其中包括 runAction 方法会将 alpha 设置为 0,取消隐藏节点,然后调用 SKAction.fadeInWithDuration 类似于下面的内容(请原谅任何语法错误,这是徒手伪代码)...

startGame()
self.fadeIn(self.myLabel, duration: 2.0)

...

func fadeIn() {
    self.myLabel.alpha = 0.0
    self.myLabel.hidden = false
    self.myLabel.runAction(SKAction.fadeInWithDuration(2.0))
}

关于ios - 隐藏时淡入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37015181/

相关文章:

objective-c - '==' 的左操作数是垃圾值

c++ - OpenGL ES 2.0 : paint in FBO + Texture = gray blending in texture

ios - CloudKit - 具有依赖性的 CKQueryOperation

objective-c - 没有隐藏项目的 Core Graphics 屏幕截图

ios - 文本对齐 drawInRect 不工作

ios - 从 url 下载图像并将其存储为文件

ios - 无论如何要在两个 View 之间使用 CATransition 时删除淡入淡出?

swift - 使用粒子效果或动画 sks 文件创建填充字母

ios - 为什么这张图片不适合屏幕尺寸?

ios - 如何从 SkView 中放松