Reference article http://cn.cocos2d-x.org/tutorial/show?id=1322
Parallaxnode is the meaning of the Parallax node, when we face a background screen, if the background can use this node when scrolling, its function is to let the child nodes have a different rate of movement, so that we can make some child nodes move slower, and some of the child nodes move faster. This creates the effect is the distant scenery movement fast, and near the scene movement fast, gives the person a more real feeling.
How to use Parallaxnode directly on the code
In the. h file definition
Parallaxnode *parallax; Parallax Node class
Sprite* bg1; Background 1
Sprite* Bg2; Background 2
void update (float DT); To use a timer if the background is automatically moved, write the moving distance in the update
BOOL Ontouchbegan (touch* Touch, event* Event);
void Ontouchmoved (touch* Touch, event* Event);
void ontouchended (touch* Touch, event* Event);
. cpp file Inside
First add in init ()
Define background 1 and background 2
BG1 = Sprite::create ("Bg.png"); Bg2 = Sprite::create ("City.png");//Parallax node definition Add parallax = parallaxnode::create (); This->addchild (parallax) ; Defines the speed point dustspeed = Point (1, 0); Point bgspeed = Point (0.5, 0); The first parameter is the node to be added,
<//The second parameter is ZOrder, which determines the order of the display,
<span style= "font-family:arial, Helvetica, Sans-serif;" > The third parameter is the rate, how this is understood, that is, if your node moves at a speed of 1, then the speed of the nodes is relative to this 1, such as Point is (0.5,0) meaning that when my parallax left 1 units, BG1 move is 0.5 units, there is no speed in the y direction,</span>
<span Style= "Font-family:arial, Helvetica, Sans-serif;" >//</span><span style= "font-family:arial, Helvetica, Sans-serif;" > The last one is coordinates, note that this coordinate is relative to the node's coordinates, not the current layer. Run up the game, we will see BG1 moving slowly, and the sprite moving fast, so that the feeling will be more real. (The distance offset from the original position of the picture) </span>
Parallax->addchild (Bg1,1,dustspeed,point (200,300));p arallax->addchild (Bg2,2,bgspeed , point (0,0));// This->scheduleupdate (); If you want to use a timer to move automatically, use the following touch event Auto listener = eventlistenertouchonebyone::create () If the touch is sliding; Listener->setswallowtouches (true); Shield the layer below Listener->ontouchbegan = Cc_callback_2 (Apage::ontouchbegan, this); listener->ontouchmoved = Cc_callback_2 (apage::ontouchmoved, this); listener->ontouchended = Cc_callback_2 (apage::ontouchended, this); _eventdispatcher->addeventlistenerwithscenegraphpriority (listener, this);
void Apage::update (float dt) {///Background auto-slide when using a timer to implement point backgroundscrollvel = A, -1000, 0);p arallax->setposition ( Parallax->getposition () +backgroundscrollvel*dt);} Parallax movement implemented using touch swipe void apage::ontouchmoved (Touch *touch, Event *unused_event) {Point touchlocation = this-> Converttouchtonodespace (touch);//Gets the current coordinates after the move, and converts from screen coordinate points to OPGL coordinate points point oldtouchlocation =converttonodespace (touch- >getpreviouslocation ());//Gets the coordinates before the move, and the difference between the point translation = touchlocation-oldtouchlocation;//moving points parallax- >setposition (Parallax->getposition () +translation);//New Location}bool Apage::ontouchbegan (Touch* Touch, Event* Event ) { return true;} void apage::ontouchended (touch* Touch, event* Event) {}
Cocos2d-x Parallax Node Parallaxnode