cocos2dx3.2 development RPG "flighting" (vii) back to the battle scene, update the character's stacking order

Source: Internet
Author: User

First, preface

So far, our combat scene has already spoken about 7788 of the character movement. However, there are still some problems that need attention.


Second, the text

In the fifth section we say that there is a very important container m_rolesarray in Flightlayer, and that it is responsible for the initialization of the following two functions.

void Flightlayer::initteam (const heromessage& h1,const heromessage& h2,const heromessage& h3) {Hero* Hero1 = Hero::create (H1.r_name,this); hero1->setposition ( -100,380); Hero1->setdespoint (Point (200,380));hero1-> Initwithmessage (H1); This->addrole (Hero1); hero* Hero2 = hero::create (h2.r_name,this); hero2->setposition ( -100,260); Hero2->setdespoint (Point (400,260)); Hero2->initwithmessage (H2); This->addrole (HERO2); hero* Hero3 = hero::create (h3.r_name,this); hero3->setposition ( -100,140); Hero3->setdespoint (Point (200,140)); Hero3->initwithmessage (H3); This->addrole (HERO3);} void Flightlayer::initmonsterdeq (deque<monstermessage> deq) {This->m_monsterdeq = DEQ;}

Looking closely at these two functions, we found that there were two new things hero class and M_monsterdeq

In fact, the hero class is a derived class of role, and Monster is also a derived class of role. When it comes to real battles (rather than walking alone), we'll talk about the difference.


If your last section has been able to successfully implement the control movement of a character, you may find that there is a serious problem that the z-axis order of role does not change, resulting in the more addchild role. This is clearly not in line with our requirements. Our request is based on the y-coordinate of role to determine who is in front of who in the post. The general y-coordinate should be the higher the level of the small, to obscure the y-coordinate large.

So we also need to make the following changes:

Idea, in the update function, to determine the Y value of each role within M_rolesarray, and to sequence, and update their z-axis order

void Flightlayer::refreshlocalzorder () {; int max_z_order = M_rolesarray.size (); if (Max_z_order <= 1) {return;} Sort (M_rolesarray.begin (), M_rolesarray.end (), Bind (&flightlayer::compareposy,this,std::p laceholders::_1, std::p laceholders::_2)); M_rolesarray.sort (Bind (&flightlayer::compareposy,this,std::p laceholders::_1,std:: placeholders::_2)); for (Auto it = M_rolesarray.begin (); It!=m_rolesarray.end (); it++) {(**it)->setlocalzorder (max_ z_order--);}}

Compareposy is a comparison of the predicate (do not understand Std::list.sort can Baidu)

BOOL Flightlayer::compareposy (role_ptr a,role_ptr b) {return (*a)->getpositiony () < (*B)->getpositiony ());}

In this way, our z-axis order can change depending on the y-coordinate of the role. To meet our requirements.


This section of content is relatively small, mainly is to explain perfect good flightlayer.

My csdn Address: http://blog.csdn.net/hezijian22

Email address: [Email protected]

If you have any questions or advice, please feel free to contact me.



cocos2dx3.2 development RPG "flighting" (vii) back to the battle scene, update the character's stacking order

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.