cocos2dx3.2 development RPG "Flighting" (ix) Essential bullets

Source: Internet
Author: User
Tags addchild

First, preface

In the last section we finished the attack, in fact, it is only the attacker's side of wishful thinking to play their own attack animation, the attacked party did not know what happened, the attackers and the attackers are not connected.

So, we've introduced bullets to this stuff.


Second, the text

At first I was in the development of the idea of using a physics engine, and then think about it or not, one is difficult to control, and the other is likely to encounter some problems (for example, two people may collide.) )

So it's will wrong, just use the update function to solve the problem.

Bullets, as the name implies, are bullets.

Class Role;class Bullet:public node{public:static bullet* createwithtarget (role* sender,role** target); bool Initwithtarget (role* sender,role** target); void setdamage (int damage); ~bullet ();p rivate:sprite* M_bullet; Role* M_target; role** m_targetptr;virtual void update (float dt); int m_damage;int m_speed; Role* M_sender;};
As you can see from the scratch file, the bullet is very simple to implement.

The main view is the Create and update functions

bullet* bullet::createwithtarget (role* sender,role** targetptr) {bullet* ret = new Bullet (); if (ret && ret-> Initwithtarget (sender,targetptr)) {ret->autorelease (); return ret;} Cc_safe_delete (ret); return nullptr;} BOOL Bullet::initwithtarget (role* sender,role** targetptr) {m_damage = 0;m_bullet = Sprite::create ("Bullet/" + sender- >getbulletimg ()); Setdamage (SENDER->GETATK ()); SetPosition (sender->getposition () + Point (0,sender-> Getcontentsize () HEIGHT/2); This->addchild (m_bullet); m_speed = Sender->getbulletspeed (); m_targetPtr = Targetptr;m_sender = Sender;this->scheduleupdate (); return true;}

The CREATE function is like this, noting that the bullets ' pictures are based on each of the different roles. (There is no bullet in the melee character, we can replace the material with a small piece of transparent)

In fact, there are many attributes in the role class. Everyone here by name should know how.

The CREATE function is set after the attacker and the attacker who has the bullet. Update is responsible for handling

void Bullet::update (float dt) {m_target = *m_targetptr;if (!m_target) {This->removefromparentandcleanup (true); return;} if (!m_target->getboundingbox (). Containspoint (This->getposition ())) {Float distance = ccpdistance (getPosition (), M_target->getposition () +vec2 (0,m_target->getcontentsize (), height), float t = distance/m_speed;float speed _x = (M_target->getpositionx ()-Getpositionx ())/T;float speed_y = (m_target->getpositiony () + m_target-> Getcontentsize (). HEIGHT/2-Getpositiony ())/T;setpositionx (Getpositionx () + speed_x); Setpositiony (GetPositionY () + speed_y);} Else{cclog ("Bullet->arr"); m_target->injured (m_effect,m_damage); This->removefromparentandcleanup (True) ;}}
If the target dies, the bullets will be clear.

If the bullet does not reach the target area, it keeps updating XY, which is similar to role.

If reached, the injured function that triggers the target and clears itself (bullets)


Okay, we're done with the bullets, let's see how a character sends bullets.

Remember why the Onbondanimationfinish function of the role class was used? It is not clear that you can see a section.

void Role::onbondanimationfinish (armature* arm,movementeventtype type,const std::string& name) {if (type = = Complete) {if (name = = "Attack") {Cclog ("attack complete");//recovery Speed m_speed = m_initspeed;m_arm->getanimation () Setspeedscale (1.0f); This->stand ();}} if (type = = START) {if (name = = "Attack") {Cclog ("SEND BULLET"); Sendbullet ();}}}
Here we just call the Sendbullet function before attacking the animation playback

void Role::sendbullet () {if (M_attacktarget && m_layer) {m_layer->addbullet (this,m_attacktargetptr);}}
and the Sendbullet function just lets M_layer (actually flightlayer) call the Addbullet function

void Flightlayer::addbullet (role* sender,role_ptr targetptr) {bullet* Bullet = bullet::createwithtarget (sender, TARGETPTR); This->addchild (bullet);}

Well, here we can automatically move the bullets to the target has been made.


The rest is the hit side, the injured function, in the injured function we can add the injury effect (the previous section did not say), as well as the calculation of the amount of blood. These are all very free. Here is my injured function for everyone to refer to

void role::injured (int effect,int damage) {runskilleffect (effect); if (Damage < 0) {m_hp-= damage;} Else{int real_damage = (damage-m_defence > 0)? damage-m_defence:1;m_hp-= real_damage;} if (m_hp > m_inithp) {m_hp = M_INITHP;} if (m_hp <= 0) {die (); return;} if (En_stat = = Role_move) {return;} moveby* ToR = moveby::create (0.2F,VEC2 (10,0)); moveby* ToL = moveby::create (0.2F,VEC2 ( -10,0)); sequence* seq;if (m_armfaceto) {seq = sequence::create (tor,tol,null);} Else{seq = Sequence::create (tol,tor,null);} M_arm->runaction (seq);}
Effect This parameter is the play effect, can be ignored.


This is the end of this section.

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" (ix) Essential bullets

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.