Cocos2d to achieve the following effect of RPG Character Map walking

Source: Internet
Author: User

Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please more advice, if you feel good please support a lot of praise. Thank you! Hopy;)

In some RPG games, the character queue when walking in the map sometimes need to achieve a follow-up effect, such as we have played the FC game << swallowed heaven and earth >>

The effect is that when the queue has only one person, ta can freely walk in the map, when the queue more than one person, we let other characters follow the game lead after walking, to achieve a "moe" trailing effect.

For example, you can see that there are 4 characters in the game queue, but we set to show only the last 2 bits, of course, followed by 100 bits is no problem, if you want to turn the RPG into the rhythm of the snake;)

Open the GameCharacter.h code and add 2 properties to the interface:

@property (nonatomic,strong) GameCharacter *followedGC;@property (nonatomic,assignCGPoint lastTilePos;

Next open the Gamecharacter.m file and modify the Popstepandanimate method as follows:

//按最短路径中每一步用动画的方式移动,使角色沿最短路径最终到达目的地-(void)popStepAndAnimate{    //原角色行走代码...    _lastTilePos = [_mh tilePosForPosition:self.position];    //原角色动画代码...    if (_followedGC) {        _followedGC.lastTilePos = _lastTilePos;        [_followedGC moveTowardByAStar:[_mh positionForTilePos:_lastTilePos]];    }}

The code function is to set the current position of the role to the previous location, and then, if there is a role to follow, call its own Move method, and the destination is the previous location. Because the previous position totals the queue header with the same role position, the first call to the next role is not moved, which is also the effect we want (otherwise, All characters will overlap and move at the same time)

Finally we add a follow method:

-(void)follow:(GameCharacter*)gc;

The implementation is as follows:

-(void)follow:(GameCharacter *)gc{    gc.followedGCself;    self.position = gc.position;}

You can see that the code is simple, simply adding itself to the followed role, and then setting its own position and being followed by the character's location.

Well, just a few lines of code, our follow-up effect is complete, pretty interesting, isn't it? ;)

Cocos2d to achieve the following effect of RPG Character Map walking

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.