A simple model for launching multiple bullets is as follows:
Four rounds of bullets:
Five bullets:
Bullet. h
#ifndef __BULLET_H__#define __BULLET_H__#include "Position.h"class Bullet{public: Bullet(const Position& from, float radius); Position getPosition() const; void setPosition(float x, float y); void setPosition(const Position& pos); float getRadius() const; float mIncrementX; float mIncrementY;private: float mRadius; Position mPosition;};#endif
Bullet. cpp
#include "Bullet.h"Bullet::Bullet( const Position& from, float radius ) : mRadius(radius) ,mPosition(from) ,mIncrementX(0) ,mIncrementY(0){}Position Bullet::getPosition() const{ return mPosition;}void Bullet::setPosition( float x, float y ){ mPosition.x = x; mPosition.y = y;}void Bullet::setPosition( const Position& pos ){ mPosition = pos;}float Bullet::getRadius() const{ return mRadius;}
Position. h
#ifndef __POSITION_H__#define __POSITION_H__struct Position{ float x; float y; Position(float x, float y) { this->x = x; this->y = y; } Position set(float x, float y) { this->x = x; this->y = y; return *this; }};#endif
HelloWorldScene. h
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "Position.h"#include "Bullet.h"#include
USING_NS_CC;using namespace std;class HelloWorld : public cocos2d::CCLayer{public: struct Increment { float incrementX; float incrementY; Increment(float incrementX, float incrementY) { this->incrementX = incrementX; this->incrementY = incrementY; } }; virtual bool init(); static cocos2d::CCScene* scene(); void menuCloseCallback(CCObject* pSender); CREATE_FUNC(HelloWorld); Increment calc(const Position& from, const Position& to, float angle, float shakeAngle, int index , int count, float speed); double D2R(double angle); double R2D(double radian); virtual void registerWithTouchDispatcher(); bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void draw(); vector
mBullets;};#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene. cpp
# Include "HelloWorldScene. h "# define PI 3.14159265USING _ NS_CC; CCScene * HelloWorld: scene () {CCScene * scene = CCScene: create (); HelloWorld * layer = HelloWorld: create (); scene-> addChild (layer); return scene;} bool HelloWorld: init () {if (! CCLayer: init () {return false;} CCSize visibleSize = CCDirector: sharedDirector ()-> getVisibleSize (); CCPoint origin = CCDirector: shareddire () -> getVisibleOrigin (); CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("Custom", "CloseSelected.png", this, menu_selector (HelloWorld: menuCloseCallback )); pCloseItem-> setPosition (ccp (origin. x + visibleSize. width-pCloseItem-> getContentSize (). width/2, origin. y + pCloseItem-> getContentSize (). height/2); CCMenu * pMenu = CCMenu: create (pCloseItem, NULL); pMenu-> setPosition (CCPointZero); this-> addChild (pMenu, 1 ); this-> setTouchEnabled (true); return true;} void HelloWorld: menuCloseCallback (CCObject * pSender) {# if (CC_TARGET_PLATFORM = platform) | (CC_TARGET_PLATFORM = platform) CCMessageBox ("You pressed the close button. windows Store Apps do not implement a close button. "," Alert "); # else CCDirector: shareddire()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0 ); # endif}/** from starting position to touch point position angle shakeAngle angle between each bullet shake angle index bullet download count total number of bullets */HelloWorld :: increment HelloWorld: calc (const Position & from, const Position & to, float angle, float shakeAngle, int index, int count, float speed) {float mMidValue = (float) count-1)/2; // uses the starting point of the bullet as the original heart, and establishes the Cartesian coordinate system // One, Two, Three, within four quadrant, and the X axis plus and minus directions, Y axis plus and minus directions, bool isInQuadrant1 = (. x> from. x) & (. y> from. y); bool isInQuadrant2 = (. x <from. x) & (. y> from. y); bool isInQuadrant3 = (. x <from. x) & (. y <from. y); bool isInQuadrant4 = (. x> from. x) & (. y <from. y); bool isOnXR = (. y = from. y) & (. x> from. x); bool isOnXL = (. y = from. y) & (. x <from. x); bool isOnYT = (. x = from. x) & (. y> from. y); bool isOnYD = (. x = from. x) & (. y <from. y); bool isZero = (. x = from. x) & (. y = from. y); // float mMidLineSlope = (from. y-. y)/(from. x-. x); // The radian value float mMidLineAngleR = atan (mMidLineSlope); // The angle value float mMidLineAngleD = R2D (mMidLineAngleR); // If the parameter is set to 2 or 3, mMidLineAngleD will be smaller than 0 if (isInQuadrant2 | isInQuadrant3) {// converts it to a positive number to facilitate mMidLineAngleD = 180 + mMidLineAngleD;} else if (isOnYT) {mMidLineAngleD = 90 ;} else if (isOnXR) {mMidLineAngleD = 0;} else if (isOnXL) {mMidLineAngleD = 180;} else if (isInQuadrant4) {// In the fourth quadrant mMidLineAngleD less than 0, convert to mMidLineAngleD = 360 + mMidLineAngleD;} else if (isOnYD) {mMidLineAngleD = 270;} else if (isZero) {return HelloWorld: Increment (0, 0 );} // float mLineAngleD = mMidLineAngleD + (mMidValue-index) * angle + shakeAngle; // float mLineAngleR = D2R (mLineAngleD); if (mLineAngleD> 360) {mLineAngleD = mLineAngleD-360;} if (mLineAngleD <0) {mLineAngleD = 360 + mLineAngleD;} // determine the position of isInQuadrant1 = (mLineAngleD> 0) & (mLineAngleD <90); isInQuadrant2 = (mLineAngleD> 90) in the Cartesian coordinate system) & (mLineAngleD <180); isInQuadrant3 = (mLineAngleD> 180) & (mLineAngleD <270); isInQuadrant4 = (mLineAngleD> 270) & (mLineAngleD <360 ); isOnXR = (mLineAngleD = 0); isOnXL = (mLineAngleD = 180); isOnYT = (mLineAngleD = 90); isOnYD = (mLineAngleD = 270 ); // increment float mIncrementX = 0 in the x and y directions; float mIncrementY = 0; float mIncrement = speed; if (isInQuadrant1) {mIncrementX = abs (mIncrement * cos (mLineAngleR )); mIncrementY = abs (mIncrement * sin (mLineAngleR);} else if (isInQuadrant2) {mIncrementX = mIncrement * cos (mLineAngleR); mIncrementY = abs (mIncrement * sin (mLineAngleR )); if (mIncrementX> 0) mIncrementX =-mIncrementX;} else if (isOnYT) {mIncrementY = speed;} else if (isOnXR) {mIncrementX = speed;} else if (isOnXL) {mIncrementX =-speed;} else if (isInQuadrant3) {mIncrementX = mIncrement * cos (mLineAngleR); mIncrementY = mIncrement * sin (mLineAngleR); if (mIncrementX> 0) mIncrementX =-mIncrementX; if (mIncrementY> 0) mIncrementY =-mIncrementY;} else if (isInQuadrant4) {mIncrementX = abs (mIncrement * cos (mLineAngleR )); mIncrementY = mIncrement * sin (mLineAngleR); if (mIncrementY> 0) mIncrementY =-mIncrementY;} else if (isOnYD) {mIncrementY =-speed;} HelloWorld :: increment mResult = HelloWorld: Increment (mIncrementX, mIncrementY); return mResult;}/** degrees to radians */double HelloWorld: D2R (double angle) {return angle/180.0 * PI;}/** radian Rotation angle */double HelloWorld: R2D (double radian) {return radian/PI * 180;} void HelloWorld :: registerWithTouchDispatcher () {CCDirector: shareddire()-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true);} bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {// touch point CCPoint mLocalTouch = pTouch-> getLocation (); CCSize mScreenSize = CCDirector: sharedDirector ()-> getWinSize (); // Position of the start Position of the bullet mFrom = Position (mScreenSize. width/2, 20); // Position of the bullet flying to Position mTo = Position (mLocalTouch. x, mLocalTouch. y); // int count = 3 for five bullets at a time; // float mRadius = 10 for the radius of the circular bullet; // float mSpeed = 5 for the speed of the bullet; // float mAngle = 50 between bullets; // float mShakeAngle = 0; for (int index = 0; index <count; index ++) {// return the incremental Increment mIncremnet = calc (mFrom, mTo, mAngle, mShakeAngle, index, count, mSpeed) of each bullet ); // instantiate a circular Bullet * pBullet = new Bullet (mFrom, mRadius); pBullet-> mIncrementX = mIncremnet. incrementX; pBullet-> mIncrementY = mIncremnet. incrementY; mBullets. push_back (pBullet);} return true;}/** draw a circular bullet */void HelloWorld: draw () {for (int index = 0; index <mBullets. size (); index ++) {Bullet * pBullet = mBullets [index]; pBullet-> setPosition (pBullet-> getPosition (). x + pBullet-> mIncrementX, pBullet-> getPosition (). y + pBullet-> mIncrementY); ccDrawCircle (ccp (pBullet-> getPosition (). x, pBullet-> getPosition (). y), pBullet-> getRadius (), 0, 70, false );}}