Hi there. I hope this message won’t be ignored as well as several my previous messages, despite they were about the issues of Cocos2dx I have found and partially fixed.
Anyway. Everybody knows that parallax effect makes game appearance more beautiful, the scene looks deeper and the customers will buy / play the game with the chance increased by 42%. So I thought that I must use in my own project. And I tried. I used the default CCParallaxNode and instatly get smack in the face: it works horribly wrong if the parent layer is being scaled.
Ok, no problem. I’ll start my own ParallaxNode with blackjack and… sprites. The only difference I made is that I multiplied the position of the parallax node’s children by inverted value of the global parallax node’s scale (meaning parallax node itself and all its parents). That’s worked fine.
But the real problem is that my parallax node, or to be more precise, its parent layer, which is a container for all game nodes in the scene, is being moved: it follows the main character, thus it stays in the center of the screen. And that’s where I’m stuck, any attempt to make the child nodes of the parallax node scale properly in the center of the screen have failed.
Here is the scheme explaining my thoughts what’s going on.
If we translate the figure from the origin to some point and then scale it, it would scale relatively to the origin - red pentagon, but we want it to scale relatively to its center - green pentagon. To do that we have to additionally translate the figure by _pos * (1.0f - scale)_
This is the code I came up to (it’s a part of ::visit method of my own ParallaxNode class which is pretty much the same as CCParallaxNode):
float x = (-pos.x + point->getOffset().x + pos.x * point->getRatio().x) * inv_scale; float y = (-pos.y + point->getOffset().y + pos.y * point->getRatio().y) * inv_scale; x += pos.x * (1.0f - scale) * point->getRatio().x; y += pos.y * (1.0f - scale) * point->getRatio().y; point->getChild()->setPosition(ccp(x,y));
As I already said, unfortunately it doesn’t work properly and I wish it does. So I hope someone can help me with this mess.