Aircraft vs. 03 aircraft firing bullets, adding enemies

Source: Internet
Author: User

Aircraft fired bullets:

The bomb screen is the essence of the Thunder game. This requires control of the trajectory of the bullet.

Make one of the simplest bullet launches, and then add a variety of barrage (in fact, the main thing is to study the bullet screen)

said is the simplest, in fact, is the most common bullet launch, producing a bullet to move OK up.

method: Make a bullet launch timer, produce bullets, bullets do Moveby Action is OK up.

Code:

GameLayer.cpp

void Gamelayer::fireschedule (float dt) {Size screensize = director::getinstance ()->getwinsize (); VEC2 Crepos = M_pplayerplane->getposition ();//The initial position of the bullet float flytime = 1.0f; Sprite * Pbullet = sprite::create ("bullet1.png");p bullet->setposition (Crepos); AddChild (Pbullet,zorder_bullet); Flytime = ((screensize.height-crepos.y)/screensize.height) *flytime;//The time of movement is related to the distance of the launch, Pbullet->runaction ( Sequence::create (Moveby::create (FLYTIME,VEC2 (0,SCREENSIZE.HEIGHT-CREPOS.Y)), Removeself::create (True), nullptr)) ;}


The enemy creates a new class, because the enemy may need to have some of their own properties to follow.

Enermysprite . h

#ifndef _enermy_sprite_h_#define _enermy_sprite_h_#include "Cocos2d.h" USING_NS_CC; typedef enum {Kenermytypenone = 0, Kenermytype1,kenermytype2,kenermytype3,kenermytype4,}enermytype;class Enermysprite:public Sprite{public:static Enermysprite * Createbytype (enermytype type); static const char * GETIMAGENAMEBYTYPE (enermytype type);p ublic: Enermysprite (); ~enermysprite ();p rivate:}; #endif


Enermysprite . cpp

#include "EnermySprite.h" Enermysprite * enermysprite::createbytype (enermytype type) {const char * pimage = Getimagenamebytype (type); Enermysprite * PRet = new Enermysprite ();p ret->initwithfile (pimage);p ret->autorelease (); return pRet;} const char * enermysprite::getimagenamebytype (Enermytype type) {Const char* pimagename = Nullptr;switch (type) {case Kenermytype1:pimagename = "Enemy1.png"; break;case kenermytype2:pimagename = "Enemy2.png"; Break;case kEnermyType3: Pimagename = "Enemy3.png"; break;case kenermytype4:pimagename = "enemy4.png"; break;default:break;} return pimagename;} Enermysprite::enermysprite () {}enermysprite::~enermysprite () {}


To add to the game:

GameLayer.h

void Gamelayer::createeneschedule (float dt) {int ran = ccrandom_0_1 () * 4 + 1; Enermytype type; switch (RAN) {case 1:type = kenermytype1;break;case 2:type = kenermytype2;break;case 3:type = kEnermyType3 ; Break;case 4:type = Kenermytype4;break;default:break;} Auto Pene = Enermysprite::createbytype (type), float x = ccrandom_0_1 () * (Director::getinstance ()->getwinsize (). Width-pene->getboundingbox (). Size.width/2) + Pene->getboundingbox (). Size.width/2;float y = Director::getInsta NCE ()->getwinsize (). Height + pene->getboundingbox (). size.height/2; float flytime = 2.0f;p ene->runaction ( Sequence::create (Moveby::create (FLYTIME,VEC2 (0,-(Director::getinstance ()->getwinsize (). Height + pEne-> Getboundingbox () size.height)), Callfuncn::create (Cc_callback_1 (gamelayer::enermymoveendcallback,this)), Removeself::create (True), nullptr), AddChild (pene);p ene->setposition (x, y); M_penermyvec.pushback (pene);} void Gamelayer::enermymoveendcallback (Node * pnode) {Enermysprite * penermy = dynamic_cast<enermysprite*> (Pnode); M_penermyvec.eraseobject (penermy);} 


again a timer, remember to add an enemy's v Ector to store the enemy.

Aircraft vs. 03 aircraft firing bullets, adding enemies

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.