3

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?

IronThrone
  • 242
  • 1
  • 10
  • 1
    Do you want both textures to be shown at the same time or transition from the first texture to the second? – nathangitter Jan 02 '17 at 18:10
  • @nathan I want both textures to be shown at the same time – IronThrone Jan 02 '17 at 20:55
  • You could either have one texture (image) that is a combination of both images, or have two separate `SKSpriteNode`'s, one with each image. – nathangitter Jan 02 '17 at 20:57
  • @nathan The main reason I am doing this I hope to fade one of the sprites out. Also I am not too okay with using two SKSpriteNode's for one character. Can't I run both SKTextures at the same time? Running actions as a child or something of that sort. – IronThrone Jan 02 '17 at 21:30
  • Would `SKAction`'s `animate(with:timePerFrame:)` action work? That should animate between multiple textures over a given time period. – nathangitter Jan 02 '17 at 21:35
  • There is nothing wrong with using 2 SKSpriteNodes to achieve this effect, you can't have 2 textures display on 1 node, and any work around may end up costing more than just using 2 SKSpriteNodes – Knight0fDragon Jan 03 '17 at 01:05
  • @nathan `animate(with:timePerFrame:)` doesn't allow two textures to be displayed on one node, it just animates between textures – IronThrone Jan 03 '17 at 02:45
  • @Knight0fDragon I am using forces to move my sprite around and there are a number of collisions, that's the main reason I believe using two `SKSpriteNode`s might cause issues with the movement – IronThrone Jan 03 '17 at 02:47
  • no, using 2 physics bodies would, second SKSpriteNode does not need a physics body – Knight0fDragon Jan 03 '17 at 02:48
  • @Knight0fDragon I just updated the question – IronThrone Jan 03 '17 at 10:52

1 Answers1

0

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



}
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44