I need to run two SKTexture actions on one sprite node. I did the following:
func subSprite (_ newSprite:SKNode) {
let firstImage = SKTexture(imageNamed: "firstImage")
let secondImage = SKTexture(imageNamed: "secondImage")
(newSprite as? SKSpriteNode)?.size = firstImage.size()
mainSprite?.sprite.physicsBody = SKPhysicsBody(texture: firstImage, size: firstImage.size())
mainSprite?.sprite.physicsBody!.categoryBitMask = mainSpriteCategory
mainSprite?.sprite.physicsBody?.allowsRotation = false
mainSprite!.sprite.physicsBody?.contactTestBitMask = enemyOneCategory | enemyTwoCategory | enemyThreeCategory
mainSprite!.sprite.physicsBody?.collisionBitMask = enemyOneCategory | enemyTwoCategory | enemyThreeCategory
let firstImageAction = SKAction.setTexture(firstImage)
let secondImageAction = SKAction.setTexture(secondImage)
let textureGroupAction = SKAction.group([firstImageAction, secondImageAction ])
newSprite.run(textureGroupAction)
}
Only the secondImage appears. The first doesn't. The main reason why I am doing this is I need to run some other actions, like fade out on the secondImage. How can I get both images to appear?
UPDATE
Based on recommendation from KnightOfDragon, I created a separate sprite. Hence I've got two sprites (same dimensions), a circle sprite using a physics body and a square sprite without a physics body. I updated the position of the sprites in the update method using the code below.
mainSprite!.squareSprite.position = mainSprite!.circleSprite.position
The sprites seem to offset in some random direction when there is collision with another physics body though the sprites have the same positions (I logged their positions at several places to ensure this). When I reduced the speed of the mainSprite
significantly (which is not what I intend to do) the offset stopped or reduced. I believe it's the speed of impact on collision with another physics body that is causing the offset. How can I solve this?
Structure your sprite like this:
var mainSprite : SKNode!
var subSprite1 = SKSpriteNode(texture:SKTexture(imageNamed: "firstImage"))
var subSprite2 = SKSpriteNode(texture:SKTexture(imageNamed: "secondImage"))
mainSprite.addChild(subSprite1)
mainSprite.addChild(subSprite2)
Then do your code like this:
func initSprite (_ newSprite:SKNode) {
(newSprite as? SKSpriteNode)?.size = firstImage.size()
mainSprite!.physicsBody = SKPhysicsBody(texture: firstImage, size: firstImage.size())
mainSprite!.physicsBody!.categoryBitMask = mainSpriteCategory
mainSprite!.physicsBody!.allowsRotation = false
mainSprite!.physicsBody!.contactTestBitMask = enemyOneCategory | enemyTwoCategory | enemyThreeCategory
mainSprite!.physicsBody!.collisionBitMask = enemyOneCategory | enemyTwoCategory | enemyThreeCategory
}