-1

I know there are several other posts about this, but my case is kinda specific, i haven't seen this one yet.

I have in my game a ball-shaped sprite, that whenever I tap on it, I would like to add a colorised version of the very same sprite but with an effect of fadeIn and fadeOut.

Going to give you an example code:

self.ball = SKSpriteNode(imageNamed: "ball")
self.ball.position = CGPoint(x: midX, y: midY)
self.ball.zPosition = 1
self.ball.size = CGSize(width: 100, height: 100)

self.touchEffect = SKSpriteNode(imageNamed: "ball")
self.touchEffect.position = CGPoint(x: 0, y: 0)
self.touchEffect.zPosition = 2
self.touchEffect.size = CGSize(width: 100, height: 100)
self.touchEffect.color = UIColor.whiteColor()
self.touchEffect.blendColorFactor = 1
self.touchEffect.alpha = 0

self.ball.addChild(self.touchEffect)
self.addChild(self.ball)

Now, up to this point... I can't even see the touchEffect sprite colorised (if I put alpha to 1), but the same color of the original sprite. Why is this?

At the touchesBegan I do something like this:

func showTapEffect() {
  let fadeIn = SKAction.fadeInWithDuration(0.3)
  let fadeOut = SKAction.fadeOutWithDuration(0.3)
  let sequence = SKAction.sequence([fadeIn,fadeOut])

  self.touchEffect.runAction(sequence)
}

I also used it in different scenarios within my very same game, and it worked. Don't know why this unique case doesn't. Any hint? If you need more example code, let me know, is not a copy paste of my current code tho, I just typed it from my memory so you might see a typo error in there. But the code is very alike.

(And my sprite isn't dark or black)

Thanks in advance.

msqar
  • 2,940
  • 5
  • 44
  • 90
  • Did you try using a color other than white? – 0x141E Sep 04 '16 at 05:20
  • Position is relative, make touch effect CGPoint.zero (midX *2 means ball touch effect is off screen) – Knight0fDragon Sep 04 '16 at 12:40
  • yes i tried redColor too but didnt work @0x141E – msqar Sep 04 '16 at 13:10
  • im actually able to see the ball without color @Knight0fDragon , the coordinate in my original code is correct. I just added the code from my mind and yes u are right.. that was a typo error on the example code – msqar Sep 04 '16 at 13:12
  • Yes you can see the ball, it is the overlay you cant see because that is off screen – Knight0fDragon Sep 04 '16 at 13:13
  • in my original code i can see both balls.. i even tested doing the overlay one a little bit smaller and both were there. I know the example code above can be wrong. What u say is that the children overlay ball should have a position 0,0.. because is relative to parent and that is correct. Please dont take that example code as the real one. – msqar Sep 04 '16 at 13:16
  • well if your example code does not match your real code, then why bother posting it. You are just wasting everybodies time by solving problems that are not yours. Post code that duplicates your problem. I am going to downvote this question due to what you told me, when the code matches your problem, I will remove the downvote. – Knight0fDragon Sep 06 '16 at 15:35
  • @Knight0fDragon is almost the same thing as my original code! i just had a typo error but that's what im currently doing, there was no need to down vote really... the reason i did that was because my code is full of other non-sense stuff that won't add anything to this question. I just edited it and fixed the typo error with the position of the children. So basically it's the same structure/code. – msqar Sep 13 '16 at 15:09
  • well if your code matched, then your code works. If you were to plug it into a new project, you will see it work (if you made alpha 1) Which means you did not provide an example that recreates your bug – Knight0fDragon Sep 13 '16 at 15:35

1 Answers1

0

I am posting this an an answer because it is too large for comments, and I can post code in here if need be.

I just reread your thing like 3 times over, You are blending with White, what color are you expecting to get? if you blend Blue and White, you get Blue, if you blend Purple and Blue, You would get Blue. If you blend Blue and Gray, you get a darker Blue. It is all percentage multiplication. I do not believe you get a blend mode to pick from with colors. How it works is it takes the color of each pixel, and on the pixel, breaks it up into R,G,B, then it takes your color, and breaks that up into R G B (lets call it CR CG CB). The math becomes (R * CR,G * CG,B * CB) on a per pixel level.

You are doing (R * 1,G * 1,B * 1) which is (R,G,B)

If you want to colorize your sprite, then you need your sprite to be a gray scale image, and use colors only when you want them to stay that color (To a degree, because blending will still apply to them, you need to figure out the math on how you want it to blend)

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44