HelloWorldScene.h 文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
void gameLogic(float dt);
void addTarget();
void spriteMoveFinished(CCNode* sender);
virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void update(float dt);
cocos2d::CCArray* _FeiBiao;
cocos2d::CCArray* _DiRen;
int ShaDiShuLiang;
char* pPMan;
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp 文件
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
_DiRen = CCArray::create();
_DiRen->retain();
_FeiBiao = CCArray::create();
_FeiBiao->retain();
ShaDiShuLiang = 0;
pPMan = NULL;
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
CCSprite* pBg = CCSprite::create("b_g.png");
pBg->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(pBg, 0);
// 添加man
CCSprite* pMan = CCSprite::create("Pao.png");
pPMan = (char*) pMan;
pMan->setPosition(ccp(pMan->getContentSize().width / 2, visibleSize.height / 2));
pMan->setRotation(CC_RADIANS_TO_DEGREES( 1.5 ));
this->addChild(pMan, 2);
// schedule_selector 监听
this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0);
this->schedule( schedule_selector(HelloWorld::update));
// 添加背景音乐
CocosDenshion::SimpleAudioEngine::sharedEngine()->
preloadBackgroundMusic("bg_music.mp3");
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("S_feibiao.mp3");
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("bg_music.mp3");
// 设置当前图层为可相应图层
this->setTouchEnabled(true);
return true;
}
void HelloWorld::gameLogic(float dt)
{
this->addTarget();
}
void HelloWorld::addTarget()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
// 首先初始化精灵
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("DiRen.png");
CCSpriteFrame* frame0 = CCSpriteFrame::createWithTexture(texture, CCRectMake(0, 30*0, 64, 30));
CCSpriteFrame* frame1 = CCSpriteFrame::createWithTexture(texture, CCRectMake(0, 30*1, 64, 30));
CCSpriteFrame* frame2 = CCSpriteFrame::createWithTexture(texture, CCRectMake(0, 30*2, 64, 30));
CCSpriteFrame* frame3 = CCSpriteFrame::createWithTexture(texture, CCRectMake(0, 30*3, 64, 30));
CCSpriteFrame* frame4 = CCSpriteFrame::createWithTexture(texture, CCRectMake(0, 30*4, 64, 30));
CCArray* animFrames = CCArray::create();
animFrames->addObject(frame0);
animFrames->addObject(frame1);
animFrames->addObject(frame2);
animFrames->addObject(frame3);
animFrames->addObject(frame4);
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);
animFrames->release();
CCSprite* pDiren = CCSprite::createWithSpriteFrame(frame0);
int minY = pDiren->getContentSize().height / 2;
int maxY = winSize.height - minY;
int rangeY = maxY - minY;
int actualY = (rand() % rangeY) + minY;
pDiren->setPosition(ccp(winSize.width + pDiren->getContentSize().width / 2, actualY));
this->addChild(pDiren, 1);
CCAnimate* animate = CCAnimate::create(animation);
pDiren->runAction(CCRepeatForever::create(animate));
pDiren->setTag(1);
_DiRen->addObject(pDiren);
// 计算移动速度
int minD = (int) 5.0;
int maxD = (int) 8.0;
int rangeD = maxD - minD;
int actualD = (rand() % rangeD) + minD;
// 初始化耗时动作
CCFiniteTimeAction* actionMove =
CCMoveTo::create((float)actualD,
ccp(0 - pDiren->getContentSize().width / 2, actualY));
CCFiniteTimeAction* actionMoveDone =
CCCallFuncN::create(this,
callfuncN_selector(HelloWorld::spriteMoveFinished));
pDiren->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
}
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
CCSprite* pSprite = (CCSprite*) sender;
this->removeChild(sender);
if (pSprite->getTag() == 1)
{
_DiRen->removeObject(pSprite);
}
else if(pSprite->getTag() == 2)
{
_FeiBiao->removeObject(pSprite);
}
}
void HelloWorld::ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
{
CCTouch* touch = (CCTouch*) touches->anyObject();
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite* pFeiBiao = CCSprite::create("FeiBiao.png");
pFeiBiao->setPosition(ccp(16, size.height / 2));
int offX = location.x - pFeiBiao->getPosition().x;
int offY = location.y - pFeiBiao->getPosition().y;
if(offX <= 0)
return ;
this->addChild(pFeiBiao, 1);
pFeiBiao->setTag(2);// 设置标示
_FeiBiao->addObject(pFeiBiao);
// 添加子弹音效
CocosDenshion::SimpleAudioEngine::sharedEngine()->
playEffect("S_feibiao.mp3");
int realX = size.width +
(pFeiBiao->getContentSize().width / 2);
float ratio = (float)offY / (float)offX;
int realY = (realX * ratio) + pFeiBiao->getPosition().y;
CCPoint realDest = ccp(realX, realY);
int offRealX = realX - pFeiBiao->getPosition().x;
int offRealY = realY - pFeiBiao->getPosition().y;
float length = sqrtf((offRealX * offRealX) + (offRealY * offRealY));
float velocity = 480 / 2;
float realMoveD = length / velocity;
CCPoint locatio = pFeiBiao->getParent()->convertToWorldSpace(pFeiBiao->getPosition());
float angle = ccpAngleSigned(ccpSub(realDest, locatio), CCPointMake(0, 1));
pFeiBiao->setRotation( CC_RADIANS_TO_DEGREES( angle ));
((CCSprite*)pPMan)->setRotation(CC_RADIANS_TO_DEGREES( angle ));
pFeiBiao->runAction( CCSequence::create( // CCRotateTo::create(0.1f, angle),
CCMoveTo::create(realMoveD, realDest),
CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),
NULL));
}
void HelloWorld::update(float dt)
{
CCArray* projectilesToDelete =
CCArray::create();
projectilesToDelete->retain();
// 碰撞算法
for (int i = 0;i < _FeiBiao->count();i++)
{
// 取出第 i 个对象,转化为精灵对象指针
CCSprite* FeiBiao = (CCSprite*)_FeiBiao->objectAtIndex(i);
// 获取这个对象的区域
CCRect FeiBiaoRect = CCRectMake(FeiBiao->getPosition().x - (FeiBiao->getContentSize().width / 4),
FeiBiao->getPosition().y - (FeiBiao->getContentSize().height / 4),
FeiBiao->getContentSize().width / 2,
FeiBiao->getContentSize().height / 2
);
// 创建存放敌人的数组
CCArray* targetsToDelete = CCArray::create();
targetsToDelete->retain();
for (int j = 0; j < _DiRen->count(); j++)
{
// 取出第 j 个对象,转化为精灵对象指针
CCSprite* DiRen = (CCSprite*)_DiRen->objectAtIndex(j);
CCRect DiRenRect = CCRectMake(DiRen->getPosition().x - (DiRen->getContentSize().width / 2) + 8,
DiRen->getPosition().y - (DiRen->getContentSize().height / 4) ,
DiRen->getContentSize().width - 25,
DiRen->getContentSize().height / 2
);
// 如果两个范围有交集
if (FeiBiaoRect.intersectsRect(DiRenRect))
{
targetsToDelete->addObject(DiRen);
_FeiBiao->removeObject(FeiBiao);
this->removeChild(FeiBiao, true);
}
}
for (int k = 0;k < targetsToDelete->count(); k++)
{
CCSprite* DiRen = (CCSprite*)targetsToDelete->objectAtIndex(k);
_DiRen->removeObject(DiRen);
this->removeChild(DiRen, true);
ShaDiShuLiang++;
}
}
if (ShaDiShuLiang > 5)
{
}
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
上图: