How to animate the opacity of a SKShapeNode?
Asked Answered
A

1

11

I am working on the instructions screen of my game and want to show to the user that they should tap a certain area of the screen.

So I want to show an animation where

1) A finger is scaling in and out of the screen, followed by -- working fine--

2) A rectangle changing opacity (to show to tap there) and then -- need help--

3) A text flashing saying, "Tap here" -- need help--

For 1), I have this (working fine):

    finger = SKSpriteNode(texture: fingerTxt)
    finger.position = CGPoint(x: 330, y: 450)
    finger.zPosition = 10
    InstHolderNode.addChild(finger)

    let fingerTapScaleDown = SKAction.scale(by: 0.6, duration: 0.7)
    let fingerTapScaleUp = SKAction.scale(by: 1.6, duration: 0.7)
    let fingerScalingSequence = SKAction.sequence([fingerTapScaleDown,fingerTapScaleUp])

    let fingerTapScaleForever = SKAction.repeatForever(fingerScalingSequence)
    finger.run(fingerTapScaleForever)

For 2), I have :

    var rect = SKShapeNode(rectOf: CGSize(width: 150.0, height: frame.height * 2))
    rect.position = CGPoint(x: 300, y: 100)
    rect.fillColor = SKColor.brown
    rect.alpha = 0.5
    InstHolderNode.addChild(rect)

Question :

How do I sync 1) and 2) such that after 1) is completed (the finger tapping animation is done), rect.alpha value to change to 0.1 and then changed back to 0.5 and then 1) happens and then rect.alpha is changed to change to 0.1 (in a loop) continuously.

Many thanks!!

Axial answered 29/9, 2016 at 16:32 Comment(7)
I was going to write an answer, but it is too much and should be something you code. You already know about sequence. At the end of your sequence you add a runBlock action to fire a sequence of actions on the rectangle that uses fadeToAlpha. Then at the end of this action, you run another block that fires the fingerScaling action. Do not use repeat forever, this will repeat forever for youEchino
Thanks for the prompt reply. Could you please guide me on how to change the alpha value of this rect, every 0.6 seconds, after the finger tapping action 1) takes place please? Thanks !Axial
already told you how to do itEchino
Hi, I am still a beginner and tried this : pastebin.com/BZBFuqsA The fading of the rectangle only happens one and not continuously. How can I make it indefinite, till I want it to stop? ThanksAxial
Hi, I even tried rect.run(fingerTapScaleForever) ) and that compiled but the game didn't work. Would be grateful if you could kindly point me in the right direction please. Many thanksAxial
Hi,sorry to bother you. I am a beginner and learning from online tutorials. Would be grateful if you could point me in the right direction please. Many thanks !Axial
Check my answerSitzmark
S
9

Try this:

    var fadeOut = SKAction.fadeAlpha(to: 0.1, duration: 0.5)
    var fadeIn = SKAction.fadeAlpha(to: 0.5, duration: 0.5)

    rect.run(SKAction.repeatForever(SKAction.sequence([fingerScalingSequence, fadeOut, fadeIn])))

Adjust the durations as per your liking

Sitzmark answered 19/10, 2016 at 15:26 Comment(1)
Hope this helpsSitzmark

© 2022 - 2024 — McMap. All rights reserved.