日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cocos2dx-是男人就坚持20s 练手项目

發布時間:2024/3/13 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cocos2dx-是男人就坚持20s 练手项目 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

前段時間心血來潮看了下app游戲方面的東西

?

,對比了下各種技術和市場招聘情況,趕腳cocos2dx在2D游戲方向還算是大有所為,遂找了幾個基礎教程看看了解了解。并附上一個簡單demo作為成果

準備工作

環境搭建倒是重頭戲,相關教程也比較多,我直接轉個給大家參考吧(安裝教程戳這里)。

開始游戲

找了個經典游戲是男人就堅持20秒,相信大家都接觸過,游戲邏輯比較簡單不外乎控制飛機躲避子彈,這里就山寨它吧

可以看到組成部分只有計時器,子彈和小鳥(為什么選小鳥呢,因為圓形圖標做碰撞檢測比較簡單,本來用飛機的,但是飛機的空白地方不好處理,簡單實例就用簡單的方法吧)

1、計時器

int time=0; CCLabelTTF* timelb;//文本框 schedule(schedule_selector(manfor20s::timecount), 1.0f);//每秒執行的計時器//每秒累加 void manfor20s::timecount(float dt) {time= time+1;CCString* ns=CCString::createWithFormat("%d", manfor20s::time);timelb->setString(ns->getCString() ); } 計時器邏輯

2、子彈的生成和碰撞檢測

CCArray* listSpirit;//獲取頁面上所有元素的容器 CCSprite* plane;//小鳥 schedule(schedule_selector(manfor20s::update));//每一幀執行void manfor20s::update(float dt) { CCSprite *pl = plane ; CCRect targetRect = CCRectMake( pl->getPosition().x - (pl->getContentSize().width/2), pl->getPosition().y - (pl->getContentSize().height/2), pl->getContentSize().width, pl->getContentSize().height); CCRect win=CCRectMake(0,0,visibleSize.width,visibleSize.height);listSpirit=this->getChildren();//獲取所有元素for (int i=listSpirit->count()-1;i>=0;i--){CCSprite* it=(CCSprite*)listSpirit->objectAtIndex(i);if(it->getTag()==2)//tag為2則為子彈 {/*CCSprite *sp = dynamic_cast<CCSprite*>(it); */CCRect projectileRect = CCRectMake( it->getPosition().x - (it->getContentSize().width/2), it->getPosition().y - (it->getContentSize().height/2), it->getContentSize().width, it->getContentSize().height);if ( ccpDistance(it->getPosition(),plane->getPosition())<15) //子彈和小鳥圓心點相距小于15則認為碰撞了 { CCMessageBox("被擊中了","alert");menuCloseCallback();//關閉break;} if(!win.intersectsRect(projectileRect))//如果子彈超出窗體則刪除 {this->removeChild(it); }}}#pragma region 產生彈道 隨機生成各個方向的子彈if(getRand(1,10)>8)//隨機因子 {//get directerint di =getRand(0,3);CCSprite * pu =CCSprite::create("p.png"); pu->setTag(2);CCPoint from;CCPoint to;switch(di){case 0://up to down {from=ccp(getRand(0,visibleSize.width),visibleSize.height);to=ccp(getRand(-visibleSize.width,visibleSize.width*2),-10); } break;case 1://down to up {from=ccp(getRand(0,visibleSize.width),0);to=ccp(getRand(-visibleSize.width,visibleSize.width*2),visibleSize.height+10);}break;case 2://left to right {from=ccp(0,getRand(0,visibleSize.height)); to=ccp(visibleSize.width+10,getRand(-visibleSize.height,visibleSize.height*2));} break;case 3://right to left {from=ccp(visibleSize.width,getRand(0,visibleSize.height)); to=ccp(-10,getRand(-visibleSize.height,visibleSize.height*2));} break;default:break;}pu->setPosition(from);this->addChild(pu);int distance=cocos2d::ccpDistance(from,to);CCActionInterval *forward = CCMoveTo::create(distance/50,to); //moveto 速度控制pu->runAction(forward); }#pragma endregion }//random int manfor20s::getRand(int start,int end) { float i = CCRANDOM_0_1()*(end-start+1)+start; //get random from start to endreturn (int)i; } 子彈的生成和碰撞檢測

3、小鳥的移動

bool manfor20s::ccTouchBegan(CCTouch * touch,CCEvent* event) {CCPoint heropos = plane->getPosition();CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);if (location.x > heropos.x - 24 && location.x < heropos.x + 24 && location.y > heropos.y - 24 && location.y < heropos.y + 24){isControl = true;deltax = location.x - heropos.x;deltay = location.y - heropos.y;}return true; }void manfor20s::ccTouchMoved(CCTouch * touch,CCEvent* event) {if (isControl){CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);float x = location.x - deltax;float y = location.y - deltay;plane->setPosition(ccp(x,y));} }void manfor20s::ccTouchEnded(CCTouch * touch, CCEvent * event) {isControl = false; } 小鳥的移動

大體邏輯就是這樣,第一次做c++項目,分不清::?? .? ->的概念,幸好項目比較小問題不大,希望有機會能接觸高大上一點的項目做做,哈哈,不知道怎么傳代碼,就吧.h文件和.cpp文件都貼上來吧

#ifndef __manfor20s_SCENE_H__ #define __manfor20s_SCENE_H__#include "cocos2d.h"class manfor20s:public cocos2d::CCLayer {public: virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointerstatic cocos2d::CCScene* scene();// a selector callbackvoid menuCloseCallback();// implement the "static node()" method manually CREATE_FUNC(manfor20s);void timecount(float dt);void update(float dt);int getRand(int start,int end) ; int time;bool isControl;int deltax;int deltay;//觸屏響應重寫這三個方法virtual bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//按下virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//拖動virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//松開 };#endif 游戲頁.h #include "manfor20s.h" #include "MainPage.h" USING_NS_CC; CCLabelTTF* timelb; CCSize visibleSize; CCArray* listSpirit; CCSprite* plane; CCScene* manfor20s::scene(){CCScene *scene = CCScene::create(); manfor20s *layer = manfor20s::create(); scene->addChild(layer); return scene; }bool manfor20s::init() {if ( !CCLayer::init() ){return false;}this->setTouchEnabled(true);CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); timelb=CCLabelTTF::create("0", "Arial", 20);timelb->setPosition(ccp(origin.x+10,origin.y +visibleSize.height-20));this->addChild(timelb); manfor20s::time=0;plane=CCSprite::create("bird.png"); plane->setTag(1);plane->setPosition(ccp(origin.x+visibleSize.width/2,origin.y + visibleSize.height/2));this->addChild(plane);schedule(schedule_selector(manfor20s::update));schedule(schedule_selector(manfor20s::timecount), 1.0f);return true; }void manfor20s::update(float dt) { CCSprite *pl = plane ; CCRect targetRect = CCRectMake( pl->getPosition().x - (pl->getContentSize().width/2), pl->getPosition().y - (pl->getContentSize().height/2), pl->getContentSize().width, pl->getContentSize().height); CCRect win=CCRectMake(0,0,visibleSize.width,visibleSize.height);listSpirit=this->getChildren();for (int i=listSpirit->count()-1;i>=0;i--){CCSprite* it=(CCSprite*)listSpirit->objectAtIndex(i);if(it->getTag()==2){/*CCSprite *sp = dynamic_cast<CCSprite*>(it); */CCRect projectileRect = CCRectMake( it->getPosition().x - (it->getContentSize().width/2), it->getPosition().y - (it->getContentSize().height/2), it->getContentSize().width, it->getContentSize().height);if ( ccpDistance(it->getPosition(),plane->getPosition())<15) { CCMessageBox("被擊中了","alert");menuCloseCallback();break;} if(!win.intersectsRect(projectileRect))//delete if over the windows {this->removeChild(it); }}}#pragma region 產生彈道 if(getRand(1,10)>8)//隨機因子 {//get directerint di =getRand(0,3);CCSprite * pu =CCSprite::create("p.png"); pu->setTag(2);CCPoint from;CCPoint to;switch(di){case 0://up to down {from=ccp(getRand(0,visibleSize.width),visibleSize.height);to=ccp(getRand(-visibleSize.width,visibleSize.width*2),-10); } break;case 1://down to up {from=ccp(getRand(0,visibleSize.width),0);to=ccp(getRand(-visibleSize.width,visibleSize.width*2),visibleSize.height+10);}break;case 2://left to right {from=ccp(0,getRand(0,visibleSize.height)); to=ccp(visibleSize.width+10,getRand(-visibleSize.height,visibleSize.height*2));} break;case 3://right to left {from=ccp(visibleSize.width,getRand(0,visibleSize.height)); to=ccp(-10,getRand(-visibleSize.height,visibleSize.height*2));} break;default:break;}pu->setPosition(from);this->addChild(pu);int distance=cocos2d::ccpDistance(from,to);CCActionInterval *forward = CCMoveTo::create(distance/50,to); //moveto 速度控制pu->runAction(forward); }#pragma endregion }void manfor20s::timecount(float dt) {manfor20s::time= manfor20s::time+1;CCString* ns=CCString::createWithFormat("%d", manfor20s::time);timelb->setString(ns->getCString() ); }int manfor20s::getRand(int start,int end) { float i = CCRANDOM_0_1()*(end-start+1)+start; //get random from start to endreturn (int)i; } //close button void manfor20s::menuCloseCallback() {this->removeAllChildren();this->unscheduleAllSelectors(); CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); // turn on display FPSpDirector->setDisplayStats(true);// set FPS. the default value is 1.0/60 if you don't call thispDirector->setAnimationInterval(1.0 / 60);// create a scene. it's an autorelease objectCCScene *pScene = MainPage::scene(); pDirector->replaceScene(pScene); }bool manfor20s::ccTouchBegan(CCTouch * touch,CCEvent* event) {CCPoint heropos = plane->getPosition();CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);if (location.x > heropos.x - 24 && location.x < heropos.x + 24 && location.y > heropos.y - 24 && location.y < heropos.y + 24){isControl = true;deltax = location.x - heropos.x;deltay = location.y - heropos.y;}return true; }void manfor20s::ccTouchMoved(CCTouch * touch,CCEvent* event) {if (isControl){CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);float x = location.x - deltax;float y = location.y - deltay;plane->setPosition(ccp(x,y));} }void manfor20s::ccTouchEnded(CCTouch * touch, CCEvent * event) {isControl = false; } 游戲頁.cpp #ifndef __MainPage_SCENE_H__ #define __MainPage_SCENE_H__#include "cocos2d.h"class MainPage : public cocos2d::CCLayer { public:// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphonevirtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointerstatic cocos2d::CCScene* scene();// a selector callbackvoid menuCloseCallback(CCObject* pSender);// a selector callbackvoid menustartGame(CCObject* pSender);// implement the "static node()" method manually CREATE_FUNC(MainPage); };#endif // __HELLOWORLD_SCENE_H__ 菜單頁.h #include "MainPage.h" #include "manfor20s.h" USING_NS_CC;CCScene* MainPage::scene() {// 'scene' is an autorelease objectCCScene *scene = CCScene::create();// 'layer' is an autorelease objectMainPage *layer = MainPage::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene; }// on "init" you need to initialize your instance bool MainPage::init() {// // 1. super init firstif ( !CCLayer::init() ){return false;}//獲取原始尺寸CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();//開始和退出按鈕CCLabelTTF *label1 = CCLabelTTF::create("Start", "Arial", 20); // create a exit botton CCMenuItemLabel *start_game = CCMenuItemLabel::create(label1, this, menu_selector(MainPage::menustartGame) ); CCLabelTTF *label2 = CCLabelTTF::create("Exit", "Arial", 20); // create a exit botton CCMenuItemLabel *exit_game = CCMenuItemLabel::create(label2, this, menu_selector(MainPage::menuCloseCallback) ); start_game->setPosition(ccp((origin.x + visibleSize.width - start_game->getContentSize().width)/2 ,origin.y+visibleSize.height/2 + start_game->getContentSize().height/2));exit_game->setPosition(ccp((origin.x + visibleSize.width - exit_game->getContentSize().width)/2 ,origin.y+visibleSize.height/2 + exit_game->getContentSize().height/2-50));// create menu, it's an autorelease objectCCMenu* pMenu = CCMenu::create(start_game,exit_game, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1);//標題CCLabelTTF* pLabel = CCLabelTTF::create("can you hold 20 sec?", "Arial", 28);// position the label on the center of the screenpLabel->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - pLabel->getContentSize().height));// add the label as a child to this layerthis->addChild(pLabel, 1);//背景圖片CCSprite* pSprite = CCSprite::create("background.jpg");pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));this->addChild(pSprite, 0);return true; }void MainPage::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"); #elseCCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)exit(0); #endif #endif }void MainPage::menustartGame(CCObject* psender) {CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();pDirector->setOpenGLView(pEGLView);// turn on display FPSpDirector->setDisplayStats(true);// set FPS. the default value is 1.0/60 if you don't call thispDirector->setAnimationInterval(1.0 / 60);// create a scene. it's an autorelease objectCCScene *pScene = manfor20s::scene(); pDirector->replaceScene(pScene); } 菜單頁.cpp

?下載代碼戳這里

?

轉載于:https://www.cnblogs.com/qyzBlog/p/3627592.html

總結

以上是生活随笔為你收集整理的cocos2dx-是男人就坚持20s 练手项目的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。