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;
};
#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.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学习笔记(19)--label 、label atlas
  • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
  • Cocos2d-x学习(一):HelloWorld
  • cocos2dx在xcode下开发,编译到android上(2)
  • cocos2d 设置屏幕默认方向
  • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
  • Cocos2d-x 2.0 之 Actions “三板斧” 之一
  • cocos2d-x学习笔记(18)--游戏打包(windows平台)
  • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1