Simple implementation of cocos2d-x scrolling Screen

Source: Internet
Author: User

1. Add two genie to the Layer

CCSprite * pSp1 = CCSprite: create ("bg01.jpg ");

CCSprite * pSp2 = CCSprite: create ("bg01.jpg ");

PSp1-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width/2, CCDirector: shareddire()-> getWinSize (). height/2 ));

PSp2-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width/2, CCDirector: shareddire()-> getWinSize (). height/2 + 576); // The height of the background image is 576.

This-> addChild (pSp1, 0, 11 );

This-> addChild (pSp2, 0, 12 );

2. Start the screen rolling timer.

This-> schedule (schedule_selector (GamingLayer: BackgroundScrollLogic), 0.1f );

3. Implementation of the scrolling callback function


Void GamingLayer: BackgroundScrollLogic (float t)

{

CCSprite * pBg1 = (CCSprite *) getChildByTag (11 );

CCSprite * pBg2 = (CCSprite *) getChildByTag (12 );


PBg1-> setPositionY (pBg1-> getPositionY ()-10); // move 10 pixels each time

PBg2-> setPositionY (pBg1-> getPositionY () + 576); // Step 2


// If the second image is rolled to the end, reset the original position of genie 1.

If (pBg2-> getPositionY () <= CCDirector: shareddire()-> getWinSize (). height/2)

{

PBg1-> setPositionY (CCDirector: shareddire()-> getWinSize (). height/2 );

}

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.