cocos2dx 基础学习(一)
# vs2010 + cocos2d-2.1beta3-x-2.1.1 项目结构 AppDelegate.cpp的 applicationDidFinishLaunching() 方法 bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);//帧率
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();//初始场景
// run
pDirector->runWithScene(pScene);
return true;
}
获得导演(单例),设置了opengl、帧率,设置了初始的启动场景 HelloWorldScene.h 的一些成员 public:
cocos2d::CCSprite* target ;
void myDefine(CCNode* who);
void createTarget();
void gameLogic(float dt);
// CClayer touch事件
void ccTouchesEnded(cocos2d::CCSet *pTouches,cocos2d::CCEvent *pEvent);
// 定义飞镖 和 怪物的集合
// array 插入 删除效率低, 查找的效率高
// list 高 低
// 添加 : 飞镖 怪物出现
// 删除 : 碰撞
// 遍历 : 每隔fps事件 就要遍历检查(多,所以选择array)
cocos2d::CCArray *_targets;
cocos2d::CCArray *_projs;
~HelloWorld();
void update(float delta); // delta - 1.0 / fps
int _successCount;
// 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 recommand to return the exactly class pointer
static cocos2d::CCScene* scene();
// a selector callback
//void menuCloseCallback(CCObject* pSender);
// 响应函数
void responseFunc(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
director – scene – layer – sprite class HelloWorld : public cocos2d::CCLayerColor // CCLayer的一个子类 ,可以设置颜色 在HelloWorld::init() 中可以初始化如下 // 初始化颜色
CC_BREAK_IF(! CCLayerColor::initWithColor(ccc4(255,255,255))); // r g b 透明度
layer中添加label,sprite,button等基本三步骤 CCSprite* player = CCSprite::create("Player.png");
player->setPosition(ccp(20,size.height/2));
this->addChild(player);
menu菜单/菜单项 如下一个场景切换的菜单项 // 得到屏幕size
CCSize size = CCDirector::sharedDirector()->getWinSize();
// 菜单的使用
CCMenuItemImage *item = CCMenuItemImage::create(
"btn_restart.png","btn_restart.png",this,menu_selector(GameOverLayer::overCallback));
item->setPosition(ccp(0,size.height/2 - 100));
CCMenu* pMenu = CCMenu::create(item,NULL);// 加入菜单
this->addChild(pMenu);
在头文件添加响应的回调函数,编写回调函数 //重玩
void GameOverLayer::overCallback(CCObject* pSender)
{
// CCDirector::sharedDirector()->end();
CCScene *helloScene = HelloWorld::scene();
CCDirector::sharedDirector()->replaceScene(helloScene); //切换场景
}
关于切换场景时的传值 CCScene* GameOverLayer::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
GameOverLayer *layer = GameOverLayer::create();
layer->setTag(100); //设置layer的tag
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
2 在切换时, CCScene *overScene = GameOverLayer::scene(); //CCScene -> GameOverLayer ->_label
GameOverLayer *overLayer = (GameOverLayer *)overScene->getChildByTag(100);
overLayer->_label->setString("You Lost!");
CCDirector::sharedDirector()->replaceScene(overScene); //切换场景
layer的touch事件 // 打开CCLayer的touch事件 this->setTouchEnabled(true);
// 重写各个touch事件 bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
void ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);
void ccTouchesCancelled(CCSet *pTouches,CCEvent *pEvent);
touch事件可以得到点击的position,判断点击的目标等 CCPoint touchPos = pTouch->getLocation();
CCTouch *touch = (CCTouch *)pTouches->anyObject();
CCPoint locInView = touch->getLocationInView(); // 这里是UI坐标系,需要转换成cocos坐标系
CCPoint loc = CCDirector::sharedDirector()->convertToGL(locInView);
给Sprite添加动画,动作 // 创建飞镖
CCSprite *proj = CCSprite::create("Projectile.png");
proj->setPosition(ccp(20,screenSize.height / 2.0));
this->addChild(proj);
// 添加一系列动作
CCMoveTo *move = CCMoveTo::create(D / 320,ccp(endx,endy));// move对象 速度 移动的终点
CCCallFuncN *moveFinish = CCCallFuncN::create(this,callfuncN_selector(HelloWorld::myDefine));
CCSequence *actions = CCSequence::create(move,moveFinish,NULL);
proj->runAction(actions); // 给精灵添加actions
定时器,刷新 this->schedule(schedule_selector(HelloWorld::update)); //每隔刷新周期重写
void HelloWorld::update(float delta) // delta - 1.0 / fps
{
//TODO
}
碰撞检测 参考学习:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |