Cocos2d-x--发射多发子弹
来源:未知 责任编辑:责任编辑 发表时间:2013-12-06 08:43 点击:次
	   
	一个简单的发射多发子弹的模型,效果图如下:
四发子弹:

五发子弹:

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;
};
#endifBullet.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;
    }
};
#endifHelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "Position.h" #include "Bullet.h" #includeUSING_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.14159265
USING_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::sharedDirector()->getVisibleOrigin();
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "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 == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}
/**
from 起始位置
to 触摸点位置
angle 每颗子弹之间的夹角
shakeAngle 子弹的抖动角度
index 子弹下载
count 子弹总数
*/
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;
    //以子弹起始点为原心,建立直角坐标系
    //一,二,三,四象限内,和X轴正负方向,Y轴正负方向,原心9种情况
    bool isInQuadrant1 = (to.x > from.x ) && (to.y > from.y);
    bool isInQuadrant2 = (to.x 
	相关新闻>>
最新推荐更多>>>
              
          - 发表评论
- 
				
- 最新评论 进入详细评论页>>

![cocos2d_x+lua[2]](/uploads/allimg/131030/110J64609-0-lp.jpg)








