1.簡單代碼分析
新建一個項目,先簡單分析一下代碼
main.cpp
int main(int argc, char **argv) { // create the application instance AppDelegate app; return Application::getInstance()->run(); }
AppDelegate執行
bool AppDelegate::applicationDidFinishLaunching() { // initialize director 創建'導演' auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLView::create("CGame"); director->setOpenGLView(glview); } // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object Scene *scene = Scene::create(); // 創建一個場景 Layer *layer = Layer::create(); // 創建一個圖層 scene->addChild(layer); // 把層添加到場景
Sprite *sprite = Sprite::create("HelloWorld.jpg"); // 創建一個精靈 sprite->setAnchorPoint(Point(0,0)); // 設置錨點為(0,0),默認值為(0.5,0.5); layer->addChild(sprite); // 添加sprite(精靈)到圖層 // run // director->runWithScene(scene); 第一次使用runWithScene director->runWithScene(scene); // director->replaceScene(); 之後使用replaceScene return true; }
2.文本的顯示與輸入
>>1.LabelTTF文本顯示標簽
Size size = Director::getInstance()->getVisibleSize(); // 獲取當前屏幕大小 LabelTTF *label = LabelTTF::create(); label->setString("Hello cocos2d-x"); label->setFontSize(36); label->setPosition(size.width / 2,size.height /2); addChild(label); // 添加精靈
>>2.TextFieldTTF文本輸入標簽
bool HelloWorld::init() { // 1. super init first if (!Layer::init()) { return false; } Size size = Director::getInstance()->getVisibleSize(); TextFieldTTF *tf = TextFieldTTF::textFieldWithPlaceHolder("input here", "宋體", 20); tf->setPosition(size.width / 2, size.height / 2); addChild(tf); auto listener = EventListenerTouchOneByOne::create(); // []函數閉包內部其實也是一個代碼塊,會訪問不到tf變量,為了訪問tf,把tf傳入給閉包代碼塊[]; listener->onTouchBegan = [tf](Touch *t, Event * e) { if (tf->getBoundingBox().containsPoint(t->getLocation())) { tf->attachWithIME(); } else { tf->detachWithIME(); } return false; }; // 添加listener Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, tf); return true; }
>>3.消息提示
3.自定義類
由於cocos2d是從objective-c擴展而來的,所以也就延續了objective-c 語言的編碼風格,下面來看一個自定義的Sprite
Ball.h
#ifndef BALL_H #define BALL_H #include "cocos2d.h" using namespace cocos2d; class Ball : public cocos2d::Sprite { public: CREATE_FUNC(Ball); // 宏構造函數,Ball::create();
virtual bool init() { initWithFile("Ball.jpg"); // objective-c中,一個初始化的方法通常以init開頭 return true; }; }; #endif /* BALL_H */
// create a scene. it's an autorelease object auto scene = Scene::create(); auto b = Ball::create(); b->setPosition(200,200); scene->addChild(b); director->runWithScene(scene);
4.TableView的使用
TableView類似於Android中的ListView,它也有Adapter(TableViewDataSource)和item點擊事件(TableViewDelegate),下面來看一個簡單的例子;
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC_EXT; USING_NS_CC; class HelloWorld : public cocos2d::Layer,TableViewDataSource,TableViewDelegate { public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); public: virtual Size cellSizeForTable(TableView *table); virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx); virtual ssize_t numberOfCellsInTableView(TableView *table); public: virtual void scrollViewDidScroll(ScrollView* view){}; virtual void scrollViewDidZoom(ScrollView* view){}; virtual void tableCellTouched(TableView* table, TableViewCell* cell); }; #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto 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() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } TableView *tv = TableView::create(this, Size(300, 300)); tv->setAnchorPoint(Point(0,0));// 設置錨點 tv->setPosition(100,0); tv->setDelegate(this); // 設置行點擊事件 addChild(tv); return true; } Size HelloWorld::cellSizeForTable(TableView *table) {// 返回每一個TableViewCell的尺寸大小 return Size(300, 50); } TableViewCell* HelloWorld::tableCellAtIndex(TableView* table, ssize_t idx) {// 返回一個TableViewCell,相當於android中BaseAdapter的getView方法 TableViewCell *cell = table->dequeueCell(); LabelTTF *label; if (cell == NULL) { cell = TableViewCell::create(); label = LabelTTF::create(); label->setTag(2); label->setFontSize(20); label->setAnchorPoint(Point(0,0)); cell->addChild(label); } else { label = (LabelTTF*) cell->getChildByTag(2); } label->setString(StringUtils::format("label %d", (int)idx)); return cell; } ssize_t HelloWorld::numberOfCellsInTableView(TableView *table) {// 返回TableView的行數 return 100; } void HelloWorld::tableCellTouched(TableView* table, TableViewCell* cell){// TableView行點擊事件 LabelTTF *label = (LabelTTF*)cell->getChildByTag(2); log("click %s",label->getString().c_str()); }
5.菜單顯示
MenuItem *n1 = MenuItemImage::create("normal.png", "press.png", [](Ref * r) { log("menu1 click"); }); MenuItem *n2 = MenuItemImage::create("press.png", "normal.png", [](Ref * r) { log("menu2 click"); }); //生成一個菜單,並且添加點擊事件 n1->setPosition(Point(s.width / 2 - 60,s.height / 2 - 40)); // 菜單1的位置 n2->setPosition(Point(s.width / 2 - 60,s.height / 2 - 100)); // 菜單2的位置 auto menu = Menu::create(n1,n2,NULL); // 生成菜單 addChild(menu);
>>.後續繼續補充。。。