5

I have been changing SKSpriteNode colour and size within didBeginContact in the following way, using an SKTexture.

if firstBody.categoryBitMask == spriteCategory && secondBody.categoryBitMask == enemyCategory    {
     var newSprite = firstBody.node
     let newImage = SKTexture(imageNamed: "newSprite.png")
     (newSprite as? SKSpriteNode)?.size = newImage.size() //magic
     oldToNewSpriteAction = SKAction.setTexture(newImage)
     newSprite!.runAction(oldToNewSpriteAction)
 }

Now I am creating a sample code without SKTextures, setting my SKSpriteNodes in the following way:

oldSprite = SKSpriteNode(color: SKColor.blueColor(), size: oldSpriteSize)
newSprite = SKSpriteNode(color: SKColor.BrownColor(), size: newSpriteSize)

How can I change the sprite colour and size within didBeginContact without SKTextures?

iGetIt
  • 695
  • 5
  • 20
  • ... just do exaclty what you are doing, but instead of newImage.size(), you use whatever size you want, and then newSprite.color = UIColor.whateverColor() – Knight0fDragon Jan 11 '16 at 18:41
  • @Knight0fDragon `newSprite = firstBody.node`, making it an `SKNode`, so when I do `newSprite.color = UIColor.blueColor()` I get an error that says `Value of type 'SKNode' has no member 'color'`. Also if I understand you well, there is no need to use `newImage`, right? – iGetIt Jan 12 '16 at 05:52
  • Because you need to cast it as an skspritenode – Knight0fDragon Jan 12 '16 at 07:48
  • 1
    Downcast with `if let newSprite = firstBody.node as? SKSpriteNode { ... }` – 0x141E Jan 12 '16 at 16:10

1 Answers1

2

This should work

newSprite.color = UIColor.redColor()
newSprite.size = CGSize(width: 250, height: 250)
Timmy Sorensen
  • 570
  • 6
  • 16