Reputation: 1918
I'm working on a confetti implementation using SKSpriteNode
and SKAction
.
Desired behavior: The confetti should sway back and forth while also falling down.
Actual behavior: The confetti falls down -- but does not sway back and forth.
What I've tried: Putting the two actions into a group
, like this:
let x = MainData.screenWidth*CGFloat.random(in: 0...1)
let testNode = SKSpriteNode(texture: MainData.confettiTexture)
let gravityAction = SKAction.move(to: CGPoint(x: x, y: -100.0), duration: 5.0)
let swaySequence = SKAction.sequence([
SKAction.move(by: CGVector(dx: -MainData.screenWidth*0.2, dy: 0.0), duration: 0.5),
SKAction.move(by: CGVector(dx: MainData.screenWidth*0.2, dy: 0.0), duration: 0.5)
])
let swayInfinitely = SKAction.repeatForever(swaySequence)
let group = SKAction.group([
swayInfinitely,
gravityAction
])
gravityAction.timingMode = .easeIn
testNode.position = CGPoint(x: x, y: MainData.screenHeight)
testNode.size = CGSize(width: MainData.screenWidth*0.05, height: MainData.screenWidth*0.05)
testNode.run(group)
My theory:
I'm guessing that this problem is happening because I'm trying to use move(to:duration:)
and move(by:duration:)
at the same time, thus supplying conflicting x-axis values.
So, how can I get my confetti to sway back and forth while also falling down? What am I doing wrong/not understanding about SKAction
?
Upvotes: 1
Views: 49
Reputation: 3857
Put the confetti nodes as child nodes of a parent SKNode. Have the parent node run the gravity action, and the confetti nodes run the sway one.
Upvotes: 1