Cocos2d-xGameTest

  • x6_728088
    了解作者
  • 113.8KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-26 10:21
    上传日期
接触cocos2d-x有几天了,确实是一个强大好用的引擎~主要原因是开源呵呵,一个简单的cocos2d-X游戏源码
Cocos2d-xGameTest.rar
  • CocosTest
  • win32
  • res
  • CocosTest.ico
    1.1KB
  • CocosTest.rc
    2.2KB
  • CocosTest.aps
    2.8KB
  • main.cpp
    400B
  • main.h
    280B
  • Classes
  • resource.h
    684B
  • AppDelegate.cpp
    2.7KB
  • HelloWorldScene.h
    996B
  • HelloWorldScene.cpp
    8.4KB
  • Test.cpp
    3.7KB
  • Test.h
    394B
  • AppDelegate.h
    1.1KB
  • Release.win32
  • Debug.win32
  • Resource
  • Images
  • target.png
    5.1KB
  • player.png
    4.2KB
  • turnpage.png
    3.8KB
  • turnpagepress.png
    17KB
  • projectile.png
    4.5KB
  • HelloWorld.png
    26.1KB
  • CloseNormal.png
    5.1KB
  • music.mid
    45.4KB
  • BOMB1.wav
    15.7KB
  • CloseSelected.png
    6KB
  • CocosTest.win32.vcxproj.user
    755B
  • CocosTest.win32.vcxproj
    7.6KB
  • CocosTest.win32.vcxproj.filters
    2.2KB
内容介绍
#include "HelloWorldScene.h" using namespace cocos2d; #include "cocos2d.h" #include "../CocosDenshion/include/SimpleAudioEngine.h" using namespace CocosDenshion; CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::node(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::node(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; //初始化变量数组 _targets = new cocos2d::CCMutableArray<cocos2d::CCSprite*>; _projectiles = new cocos2d::CCMutableArray<cocos2d::CCSprite*>; ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_IF_RETURN_F(!CCLayerColor::initWithColor(ccc4(255,255,255,255))); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object. CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); CC_IF_RETURN_F(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); //// Add the menu to HelloWorld layer as a child layer. //// Create a menu with the "close" menu item, it's an auto release object. CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL); pMenu->setPosition(CCPointZero); CC_IF_RETURN_F(! pMenu); this->addChild(pMenu, 1); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite *player = CCSprite::spriteWithFile("Images/Player.png", CCRectMake(0, 0, 27, 40) ); player->setPosition( ccp(player->getContentSize().width/2, winSize.height/2) ); this->addChild(player); //时间表选择,相当于定时器一直存在的一个线程,前面的参数是调用的函数,后面的参数是时间,单位秒 this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 ); this->schedule( schedule_selector(HelloWorld::update)); //添加支持触摸 this->setIsTouchEnabled(true); //添加背景音乐 SimpleAudioEngine::sharedEngine()->playBackgroundMusic( "music.mid"); bRet = true; return bRet; } //游戏逻辑 void HelloWorld::gameLogic(float dt) { this->addTarget(); } void HelloWorld::menuCloseCallback(CCObject* pSender) { // "close" menu item clicked CCDirector::sharedDirector()->end(); } void HelloWorld::addTarget() { CCSprite *target = CCSprite::spriteWithFile("Images/Target.png", CCRectMake(0,0,27,40) ); // Determine where to spawn the target along the Y axis CCSize winSize = CCDirector::sharedDirector()->getWinSize(); int minY = target->getContentSize().height/2; int maxY = winSize.height - target->getContentSize().height/2; int rangeY = maxY - minY; //srand( GetTickCount() ); int actualY = ( rand() % rangeY ) + minY; // Create the target slightly off-screen along the right edge, // and along a random position along the Y axis as calculated target->setPosition( ccp(winSize.width + (target->getContentSize().width/2), actualY) ); target->setTag(1); _targets->addObject(target); this->addChild(target); // 确定目标的速度 int minDuration = (int)2.0; int maxDuration = (int)4.0; int rangeDuration = maxDuration - minDuration; //srand( GetTickCount() ); int actualDuration = ( rand() % rangeDuration ) + minDuration; // Create the actions创造行为 CCFiniteTimeAction* actionMove = CCMoveTo::actionWithDuration( (ccTime)actualDuration, ccp(0 - target->getContentSize().width/2, actualY) ); CCFiniteTimeAction* actionMoveDone = CCCallFuncN::actionWithTarget( this, callfuncN_selector(HelloWorld::spriteMoveFinished)); target->runAction( CCSequence::actions(actionMove, actionMoveDone, NULL) ); } // cpp with cocos2d-x void HelloWorld::spriteMoveFinished(CCNode* sender) { CCSprite *sprite = (CCSprite *)sender; this->removeChild(sprite, true); if (sprite->getTag() == 1) // target { _targets->removeObject(sprite); } else if (sprite->getTag() == 2) // projectile { _projectiles->removeObject(sprite); } } void HelloWorld::ccTouchesEnded( cocos2d::CCSet* touches, cocos2d::CCEvent* event ) { // Choose one of the touches to work with CCTouch* touch = (CCTouch*)( touches->anyObject() ); CCPoint location = touch->locationInView(touch->view()); location = CCDirector::sharedDirector()->convertToGL(location); // Set up initial location of projectile CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite *projectile = CCSprite::spriteWithFile("Images/Projectile.png", CCRectMake(0, 0, 20, 20)); projectile->setPosition( ccp(20, winSize.height/2) ); // Determinie offset of location to projectile int offX = location.x - projectile->getPosition().x; int offY = location.y - projectile->getPosition().y; // Bail out if we are shooting down or backwards if (offX <= 0) return; projectile->setTag(2); _projectiles->addObject(projectile); // Ok to add now - we've double checked position this->addChild(projectile); // Determine where we wish to shoot the projectile to int realX = winSize.width + (projectile->getContentSize().width/2); float ratio = (float)offY / (float)offX; int realY = (realX * ratio) + projectile->getPosition().y; CCPoint realDest = ccp(realX, realY); // Determine the length of how far we're shooting int offRealX = realX - projectile->getPosition().x; int offRealY = realY - projectile->getPosition().y; float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY)); float velocity = 480/1; // 480pixels/1sec float realMoveDuration = length/velocity; // Move projectile to actual endpoint projectile->runAction( CCSequence::actions( CCMoveTo::actionWithDuration(realMoveDuration, realDest), CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) ); //添加碰撞音乐 SimpleAudioEngine::sharedEngine()->playEffect("BOMB1.wav"); } HelloWorld::~HelloWorld() { if (_targets) { _targets->release(); _targets = NULL; } if (_projectiles) { _projectiles->release(); _projectiles = NULL; } } void HelloWorld::update( float dt ) { CCMutableArray<CCSprite*> *projectilesToDelete = new CCMutableArray<CCSprite*>; CCMutableArray<CCSprite*>::CCMutableArrayIterator it, jt; for (it = _projectiles->begin(); it != _projectiles->end(); it++) { CCSprite *projectile =*it; CCRect projectileRect = CCRectMake( projectile->getPosition().x - (projectile->getContentSize().width/2), projectile->getPosition().y - (projectile->getContentSize().height/2), projectile->getContentSize().width, projectile->getContentSize().height); CCMutableArray<CCSprite*>*targetsToDelete =new CCMutableArray<CCSprite*>; for (jt = _targets->begin(); jt != _targets->end(); jt++) { CCSprite *target =*jt; CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); //如果两个位置交叉 if (CCRect::CCRectIntersectsRect(projectileRect, tar
评论
    相关推荐
    • Cocos2d Game
      I create a Cocos2d game
    • cocos2d富文本
      cocos2d 富文本实现 格式 [i:图片路径|-] [s:22|c:00ff00ff]***[-] [s:22]***[-] [l:item/1001]...[-] 随意组合嵌套
    • Cocos2d学习文档
      Cocos2d学习文档 适合新手 第1章-介绍cocos2d.pdf 第2章-开始学习.pdf 。。。。 第4章-你的第一个游戏(修正错误).pdf 。。。。
    • Cocos2d学习资料
      Cocos2D目录: 第一章:介绍Cocos2d 第二章: 开始学习 第三章: 基础知识 第四章: 你的第一个游戏(修正错误) 第五章: 游戏构成要素 第六章: 深入了解精灵(Sprite) 第七章: 横向滚屏射击游戏 第八章: 完成滚屏射击游戏...
    • cocos2d demo
      本资源是一个小的经典的cocos2d-x的demo,后续每周更新增加功能的版本
    • cocos2d精灵教程
      (译)cocos2d精灵教程:第一部分.pdf (译)cocos2d精灵教程:第三部分.pdf (译)cocos2d精灵教程:第二部分.pdf (译)cocos2d菜单教程:第一部分.pdf (译)cocos2d菜单教程:第三部分(完).pdf (译)...
    • cocos2d模板
      cocos2d-template-updated
    • Cocos2d手势
      为实现Cocos2d手势支持,改写的CCLayer、CCNode、CCGestureRecognizer三个源码类,可以参考我博客使用!
    • Cocos2D文档
      Cocos2D开发参考文档 Cocos2D帮助文档
    • cocos2d实例
      cocos的一些实例代码,有助于初学者。