Cocos2d-x 3.0 game instance learning notes "card tower guard" Step 10 --- fire bullets & monsters injured --- all source code and resources end download

Source: Internet
Author: User
Tags addchild

New entry C ++ cocos2d-x3.0 tower anti-instance game

Well, at this point, the entire game is basically finished, because of its own level, a lot of details are not well handled during the rewriting process.

The game leaves extensible code in many places, but time is not enough... And there is no artist

Note content:

1. Train of Thought, code, and Result Display

2. complete code & Resource download

I. Ideas & code

The following content is basically in the wood book, and there is nothing to modify.
Like in many places before, a hero can take a bullet for attack by detaching the bullets and binding them to the hero.
This also facilitates subsequent expansion... after all, this game is basic in many places, but it is also reserved for scalable design.
First, a base class of a bullet is used for subsequent expansion. For example, after the hero upgrade, the bullet has changed-but this game has not been completed so much.

# Define speed_default 10 class bulletbase: public entity {public: bulletbase ();~ Bulletbase (); // bind the target void lockaim (entity * entity); // obtain the target entity * getaim (); void setused (bool isused); bool isused (); // whether the bullet attacks bool isarrive (); protected: // The attack target is called. The subclass implements virtual void onlockaim (entity * aim) = 0; bool _ isarrive; bool _ isused; entity * _ aim; cc_synthesize (INT, _ atkvalue, atkvalue); // attack force cc_synthesize (INT, _ speed, speed); // speed };
Implementation

BulletBase::BulletBase(){_isUsed = false;_aim = NULL;_speed = SPEED_DEFAULT;_atkValue = 1;}BulletBase::~BulletBase(){CC_SAFE_RELEASE(_aim);}void BulletBase::setUsed(bool isUsed){_isUsed = isUsed;setVisible(isUsed);}bool BulletBase::isUsed(){return _isUsed;}void BulletBase::lockAim(Entity* entity){if(entity != NULL){CC_SAFE_RETAIN(entity);CC_SAFE_RELEASE(_aim);_aim = entity;onLockAim(_aim);}}Entity* BulletBase::getAim(){return _aim;}bool BulletBase::isArrive(){return _isArrive;}
So here we will implement a normal bullet.

class BulletNormal : public BulletBase{public:BulletNormal(); ~BulletNormal();CREATE_FUNC(BulletNormal);virtual bool init();protected:virtual void onLockAim(Entity* entity);private:void moveEnd();};
Easy to implement

# Define speed_normal 5 bulletnormal: bulletnormal () {_ speed = speed_normal;} bulletnormal ::~ Bulletnormal () {} bool bulletnormal: Init () {sprite * sprite = sprite: Create ("Sprite/bullet/bulletnor.png"); bindsprite (sprite); Return true ;} void bulletnormal: onlockaim (entity * aim) {_ isarrive = false; // here is the moving auto moveTo = moveTo: Create (0.5f, aim-> getposition (); // The callback function auto callfunc = callfunc: Create (this, callfunc_selector (bulletnormal: moveend) after the action is completed; Auto action = sequence:: Create (moveTo, callfunc, null); this-> runaction (action);} void bulletnormal: moveend () {_ isarrive = true ;}
The bullet is just a simple genie, moving from a hero to a locked target. Here it is just a few separate bullets. We should create a bullet manager and then bind it to the hero.

# Define bullet_num 10 # define bullet_time 0.07 fclass bulletmanager: Public node {public: bulletmanager ();~ Bulletmanager (); static bulletmanager * Create (); bool Init (); // virtual void onexit (); // ** 10 ** from all bullets, marry a unused bulletbase * getanyunusedbullet (); Private: vector <bulletbase *> _ bulletlist; // reserve the parent here to keep the Cocos tree of the bullet node, to be displayed void createbullets (node * parent); // update function void bulletlogiccheck (float DT );};
The bullet Manager also has a simple update function, which enables the bullet to move from the hero's position to the monster.
Then, hide it after hitting it, and then play the second bullet.

Bulletmanager: bulletmanager () {} bulletmanager ::~ Bulletmanager () {} bulletmanager * bulletmanager: Create () {bulletmanager * bulletmgr = new bulletmanager (); If (bulletmgr & bulletmgr-> Init ()) {bulletmgr-> autorelease ();} else {cc_safe_delete (bulletmgr);} return bulletmgr;} bool bulletmanager: Init () {node * parent = Director ctor: getinstance () -> getrunningscene (); createbullets (parent); this-> schedule (schedule_selector (bulletmanager: bulletlogiccheck), bullet_time); R Eturn true;} void bulletmanager: createbullets (node * parent) {for (INT I = 0; I <bullet_num; I ++) {bulletbase * bullet = bulletnormal :: create (); bullet-> setused (false); _ bulletlist. pushback (bullet); parent-> addchild (bullet, 10) ;}} void bulletmanager: bulletlogiccheck (float DT) {for (Auto bullet: _ bulletlist) {// The bullet in use if (bullet! = NULL & bullet-> isused () {// get the target to be locked, and then let it hurt entity * aim = bullet-> getaim (); If (aim! = NULL) {If (bullet-> isarrive () {Aim-> hurtme (bullet-> getatkvalue (); // hit the enemy, hide bullet-> setused (false) ;}}} bulletbase * bulletmanager: getanyunusedbullet () {for (Auto bullet: _ bulletlist) {If (bullet! = NULL & bullet-> isused () = false) {bullet-> setused (true); Return bullet ;}} return NULL ;}
Add a bullet manager to the hero.

//**10**_bulletMgr = BulletManager::create();this->addChild(_bulletMgr);
At the same time, the hero needs to launch a bullet instead of printing the ATK text.

Void hero: ATK () {cclog ("ATK !! "); Bulletbase * bullet = _ bulletmgr-> getanyunusedbullet (); If (bullet! = NULL) {bullet-> setposition (getposition (); bullet-> setatkvalue (getcuratk (); bullet-> lockaim (_ atkmonster);} _ isatkcooldown = true; // after the attack interval, cool down to this-> scheduleonce (schedule_selector (hero: atkcolldownend), getatkspeed ()/1000.0f );}
The hero is under attack, but the monster is not hurt yet and won't die. You can't see it hurt .....
So let's look at the modification of the monster's injured blood records, the same design as the bullets bound to the hero.

Class widgethpslot: Public node {public: widgethpslot ();~ Widgethpslot (); static widgethpslot * Create (entity * entity); bool Init (entity * entity); controlslider * getslider (); Private: entity * _ entity; // ** control controlslider * _ slider ;};----------------. cppwidgethpslot: widgethpslot () {_ entity = NULL; _ slider = NULL;} widgethpslot ::~ Widgethpslot () {response (_ entity); cc_safe_release (_ slider);} widgethpslot * widgethpslot: Create (entity * entity) {widgethpslot * hpslot = new widgethpslot (); if (hpslot & hpslot-> Init (entity) {hpslot-> autorelease () ;}else {cc_safe_delete (hpslot);} return hpslot;} bool widgethpslot :: init (entity * entity) {cc_safe_retain (entity); _ entity = entity; _ slider = controlslider: Create ("widget/sliderbg.png", "widget/slidervalue.png ", "widget/sliderthumb.png"); _ slider-> settouchenabled (false); this-> addchild (_ slider); Return true;} controlslider * widgethpslot: getslider () {return _ slider ;}
Then, monster adds members and overrides the functions of the base class. These functions are called separately when they are killed, bound, or injured, and add extension functions to you.
void Monster::onDead(){this->removeAllChildrenWithCleanup(true);}void Monster::onBindSprite(){if(_hpSlot == NULL){Point pos = getPosition();_hpSlot = WidgetHPSlot::create(this);_hpSlot->setPosition(pos.x,pos.y+getSprite()->getContentSize().height/2);_hpSlot->getSlider()->setMaximumValue(getHP());_hpSlot->getSlider()->setMinimumValue(0);_hpSlot->getSlider()->setValue(getHP());this->addChild(_hpSlot);}}void Monster::onHurt(int hurtValue){if(_hpSlot != NULL){int curValue = _hpSlot->getSlider()->getValue();curValue -= hurtValue;if(curValue < 0){curValue = 0;}_hpSlot->getSlider()->setValue(curValue);}}
Here, you need to put the binding genie in the previous init function behind the set attribute. Otherwise, bingsprite will put the blood volume in the onbindsprite function.
The value is 1 ......

The effect is as follows:
After the monsters are wiped out, they will be gone .....



II:

---------------------------------------

Code Completion & Resources

---------------------------------------

Well, we can only do this here .... Do not spray


Okay, personal ignorance. Please comment and discuss it with me.

Cocos2d-x 3.0 game instance learning notes "card tower guard" Step 10 --- fire bullets & monsters injured --- all source code and resources end download

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.