How to move the screen in the cocos2dx horizontal game with the genie

Source: Internet
Author: User

In a horizontal game, to move the screen as the genie moves, you must first understand the changes and changes in the problem:

Unchanged: map location;

Change: the location of the genie, the location of the screen (or the location of the layer;

Therefore, a rolling map is actually a layer rolling;


First, you must know the two methods of map (CCTMXTileMap:

1. map-> getMapSize (): number of map Blocks

2. map-> getTileSize (): the width and height of each graph Block

Further analysis changes:

The genie moves first, and the screen is adjusted according to the coordinates of the genie moves;



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + samples/samples + q8HptcTX + samples/VwdTIy6OsxuS0 + samples + 6zUKx36Osy/ pushed + pushed + q8HptcS0udax1/pushed + q8Hp1/pushed/P1Mi709DS1M/CyP3W1sfpv/pushed/ycTc0tG + pushed/pushed + pushed PC9wPgo8cHJlIGNsYXNzPQ = "brush: java; "> void GameLayer: update (float dt) {this-> _ hero-> updateDesiredPosition (dt ); // set the hero position float posX = MIN (this-> _ tilemp-> getMapSize (). width * this-> _ tilemp-> getTileSize (). width-this-> _ hero-> _ centerToSides, MAX (this-> _ hero-> _ centerToSides, this-> _ hero-> _ desiredPosition. x); float posY = MIN (3 * this-> _ tilemp-> getTileSize (). height + this-> _ hero-> _ centerToBottom, MAX (_ hero-> _ centerToBottom, _ hero-> _ desiredPosition. y); this-> _ hero-> setPosition (ccp (posX, posY); // map rolling CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); int x = MAX (_ hero-> getPositionX (), winSize. width/2); int y = MAX (_ hero-> getPositionY (), winSize. height/2); x = MIN (x, (this-> _ tilemp-> getMapSize (). width * this-> _ tilemp-> getTileSize (). width)-winSize. width/2); CCPoint actualPosition = ccp (x, y); CCPoint centerOfView = ccp (winSize. width/2, winSize. height/2); CCPoint viewPoint = ccpSub (centerOfView, actualPosition); this-> setPosition (viewPoint );}}


Code reference, from: Example.


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.