'Change alpha mask when texture changes on same SKSpriteNode
I have two functions that help me show different animations on my SKSpriteNode, but I need the alpha mask of this Sprite to change whenever the texture changes and I cannot seem to find a way to do it.
var atlasAnimation = [SKTexture]()
func loadAnimation() {
let spriteAtlas = SKTextureAtlas(named: "atlasImages)")
for index in 1...spriteAtlas.textureNames.count{
let imgName = String(format: "atlasImages%01d", index) //different images that I need with their respective alpha mask
atlasAnimation += [spriteAtlas.textureNamed(imgName)]
}
}
func runAnimation() {
animationNode = self.childNode(withName: "spriteWhatever") as? SKSpriteNode
if (animationNode != nil) {
let animation = SKAction.animate(with: atlasAnimation, timePerFrame: 0.1)
let forever = SKAction.repeatForever(animation)
animationNode?.run(forever)
}
}
I've been searching a bit and found out how to do the alpha mask thing with code instead of the checkbox on Xcode’s sidebar (which only helps setting the alpha mask with that one texture) and I've found out how to do it with a normal SKTexture. But for this animation I need a [SKTexture] and it just won´t work overriding the normal physicsBody texture atribute, since it expects a SKTexture argument and not a [SKTexture].
animationNode.physicsBody = SKPhysicsBody(texture: atlasAnimation, size: atlasAnimation.size())
//so atlasAnimation can't be placed here in "texture"
Any ideas?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|